Skip to content

Syntax of memory-freeing APIs #112

@dthaler

Description

@dthaler

Today, APIs that free memory follow the POSIX standard model for free(), etc., namely:

  1. The argument is the pointer to free, and
  2. null is permitted

In microsoft/ebpf-for-windows#2783 (comment), @gtrevi asks:

Could we merge nulling the pointer into cxplat_free_preemptible_work_item?

That is, Gianni asks about changing point 1 above, to take an [in,out] pointer to the pointer, and set it to null after freeing.

In #109, @nibanks also asks about changing point 2 above. There, Dave Thaler responded:

When we went through the decision process, we came to the opposite conclusion (ALL free apis should accept nullptr), for mainly the following reasons:

  1. Consistency with the POSIX standard. This is the industry consensus on good API design, which is why free() is defined to accept a null ptr. Thus, programmers familiar with standard API conventions aren't surprised.
  2. Minimizes number of code paths (reducing code complexity and increasing code coverage), since otherwise many places, instead of 1, have to test for null.

The first point applies to Gianni's question as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions