__BUILTIN_OBJECT_SIZE(3)
okay so bestie, welcome to the __BUILTIN_OBJECT_SIZE page β itβs a section 3 manual page from the Library Functions Manual. itβs giving βcompiler magic that tells you how big a thing is,β and honestly? iconic. πβ¨
πΈ NAME πΈ
__builtin_object_size β return the size of the given object. thatβs the whole assignment, no cap. π
β¨ SYNOPSIS β¨
size_t
__builtin_object_size(void *ptr, int type);
π¦ DESCRIPTION π¦
The __builtin_object_size() function is a clang(1) and gcc(1) built-in function that returns the size of the object referenced by ptr if known at compile time. so yeah, the compiler is doing its little detective thing before runtime, and we love that for it. ππ
If the ptr expression has any side effects, then they will not be evaluated. like, not even a little bit β the side effects just do not happen, periodt. π«Ά
π«Ά RETURN VALUES π«Ά
If the size of the object is not known or the ptr expression has side effects, the __builtin_object_size() function returns:
(size_t)-1for type0and1.(size_t)0for type2and3.
If the size of the object is known, then the __builtin_object_size() function returns the maximum size of all the objects that the compiler knows can be pointed to by ptr when type & 2 == 0, and the minimum size when type & 2 != 0. yes, that bitmask check is doing exactly what it looks like, bestie. π§β¨
π SEE ALSO π
π HISTORY π
The __builtin_object_size() appeared in GCC 4.1. a lil throwback, but make it compiler lore. β
π CAVEATS π
bestie, listen: this is a non-standard, compiler-specific extension. so donβt expect every random toolchain on the timeline to know the assignment. π
Note that currently the object size calculation pass is only done at -O1 or above, meaning that this function always returns -1 when the optimizer is off. thatβs right β optimizer off means -1 every time, no cap. π
There are some discussions about always doing the object size pass, but the issue is that without the optimization pass data sizes are not going to be correct. so itβs not just being dramatic for fun β the numbers genuinely get messy. π
For that reason code fortification (size-checked replacement functions) is currently disabled when optimization is off. so yeah, the fortified vibe is sitting this one out when -O0 shows up. π«Ά
π thatβs the whole page, bestie. this one came from Debian, dated April 27, 2024 β mwah, slay, and may your object sizes be known at compile time. π π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π the page's license: BSD-2-Clause
- Copyright (c) 2007 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms