4 NOTE: Some of the interfaces described here (most notably the allocator) are
5 semi-internal interfaces and as such API is not considered stable.
7 Common Macros and Inline Functions
8 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 -------------------------------------------------------------------------------
12 #include <core/GP_Common.h>
30 GP_CONTAINER_OF(ptr, structure, member);
32 -------------------------------------------------------------------------------
34 These common macros implements basic functions such as minimum, maximum,
35 absolute value, swap and sign.
37 All macros use 'typeof()' in order to evaluate their arguments exactly once.
39 The 'GP_ARRAY_SIZE()' macro computes size of statically defined array (i.e.
40 returns +sizeof(array) / sizeof(elem)+).
42 The 'GP_CONTAINER_OF()' macro computes pointer to a structure given pointer to
46 -------------------------------------------------------------------------------
47 #include <core/GP_Clamp.h>
50 * Clamps integer value to 8 bit unsigned value.
52 GP_CLAMP_INT_0_255(val);
55 * Clamps integer value.
57 GP_CLAMP(val, min, max);
59 -------------------------------------------------------------------------------
61 Value clamping macros.
63 NOTE: this header is not included by including the 'GP.h' header.
65 Temporary Buffer Allocator
66 ~~~~~~~~~~~~~~~~~~~~~~~~~~
68 Temporary buffer allocator is used to allocate temporary buffer needed for
69 certain operations (mostly used in image filters).
71 The intended usage of temporary buffers is:
73 * Count sum of the size needed for all buffers
74 * Allocate temporary buffer of this size
75 * Partition the buffer into smaller blocks
76 * Use the blocks as needed
77 * Once operation is done, free the buffer
79 The allocator code greatly simplifies these steps. Moreover it avoids memory
80 fragmentation by creating small buffers on the process stack (current theshold
81 is set to 2kB) and by grouping the temporary buffers into one continuous
84 NOTE: The allocator itself does not align the resulting blocks. It's your
85 responsibility to allocate the buffers in a way that the result is
86 adequately aligned (hint: the start of the block is aligned, so
87 get blocks that needs to be aligned first).
90 -------------------------------------------------------------------------------
91 #include <core/GP_TempAlloc.h>
94 * A macro that creates block allocator.
96 * The name must be unique among variable and functions names.
98 * The bsize is actual size of the block allocated.
100 GP_TempAllocCreate(name, bsize);
103 * A macro that returns pointer to the start of a block of a bsize
104 * partioned from the block allocator passed as self argument.
106 GP_TempAllocGet(self, bsize);
109 * Free the allocator memory.
111 GP_TempAllocFree(self);
112 -------------------------------------------------------------------------------
114 Example usage of the allocator:
117 -------------------------------------------------------------------------------
118 #include <core/GP_TempAlloc.h>
122 GP_TempAllocCreate(tmp, 3 * img->width);
124 uint8_t *R = GP_TempAllocGet(tmp, img->width);
125 uint8_t *G = GP_TempAllocGet(tmp, img->width);
126 uint8_t *B = GP_TempAllocGet(tmp, img->width);
128 /* start of the code that uses the buffers */
133 GP_TempAllocFree(tmp);
139 /* end of the code that uses the buffers */
141 GP_TempAllocFree(tmp);
145 -------------------------------------------------------------------------------