Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support __builtin_alloca_with_align #88

Open
rinon opened this issue May 2, 2019 · 1 comment
Open

Support __builtin_alloca_with_align #88

rinon opened this issue May 2, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@rinon
Copy link
Contributor

rinon commented May 2, 2019

No description provided.

@rinon rinon added the enhancement New feature or request label May 2, 2019
@rinon
Copy link
Contributor Author

rinon commented May 2, 2019

From GCC docs:

The __builtin_alloca_with_align function must be called at block scope. The function allocates an object size bytes large on the stack of the calling function. The allocated object is aligned on the boundary specified by the argument alignment whose unit is given in bits (not bytes). The size argument must be positive and not exceed the stack size limit. The alignment argument must be a constant integer expression that evaluates to a power of 2 greater than or equal to CHAR_BIT and less than some unspecified maximum. Invocations with other values are rejected with an error indicating the valid bounds. The function returns a pointer to the first byte of the allocated object. The lifetime of the allocated object ends at the end of the block in which the function was called. The allocated storage is released no later than just before the calling function returns to its caller, but may be released at the end of the block in which the function was called.

For example, in the following function the call to g is unsafe because when overalign is non-zero, the space allocated by __builtin_alloca_with_align may have been released at the end of the if statement in which it was called.

void f (unsigned n, bool overalign)
{
  void *p;
  if (overalign)
    p = __builtin_alloca_with_align (n, 64 /* bits */);
  else
    p = __builtin_alloc (n);

  g (p, n);   // unsafe
}

Since the __builtin_alloca_with_align function doesn’t validate its size argument it is the responsibility of its caller to make sure the argument doesn’t cause it to exceed the stack size limit. The __builtin_alloca_with_align function is provided to make it possible to allocate on the stack overaligned arrays of bytes with an upper bound that may be computed at run time. Since C99 Variable Length Arrays offer the same functionality under a portable, more convenient, and safer interface they are recommended instead, in both C99 and C++ programs where GCC provides them as an extension. See Variable Length, for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant