src/cx/allocator.h

changeset 465
1e3cb39815f8
parent 461
005c2791c2e2
child 484
9e6900b1cf9d
equal deleted inserted replaced
464:7fafc95968fc 465:1e3cb39815f8
42 /** 42 /**
43 * The class definition for an allocator. 43 * The class definition for an allocator.
44 */ 44 */
45 typedef struct { 45 typedef struct {
46 /** 46 /**
47 * Allocate \p n bytes of memory. 47 * The allocator's malloc() implementation.
48 *
49 * @param data the allocator's data
50 * @param n the number of bytes
51 * @return a pointer to the allocated memory
52 */ 48 */
53 void *(*malloc)(void *data, size_t n); 49 void *(*malloc)(void *data, size_t n);
54 50
55 /** 51 /**
56 * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long. 52 * The allocator's realloc() implementation.
57 * This function may return the same pointer that was passed to it, if moving the memory
58 * was not necessary.
59 *
60 * \note Re-allocating a block allocated by a different allocator is undefined.
61 *
62 * @param data the allocator's data
63 * @param mem pointer to the previously allocated block
64 * @param n the new size in bytes
65 * @return a pointer to the re-allocated memory
66 */ 53 */
67 void *(*realloc)(void *data, void *mem, size_t n) 54 void *(*realloc)(void *data, void *mem, size_t n)
68 __attribute__((__warn_unused_result__)); 55 __attribute__((__warn_unused_result__));
69 56
70 /** 57 /**
71 * Allocate \p nelem elements of \p n bytes each, all initialized to zero. 58 * The allocator's calloc() implementation.
72 *
73 * @param data the allocator's data
74 * @param nelem the number of elements
75 * @param n the size of each element in bytes
76 * @return a pointer to the allocated memory
77 */ 59 */
78 void *(*calloc)(void *data, size_t nelem, size_t n); 60 void *(*calloc)(void *data, size_t nelem, size_t n);
79 61
80 /** 62 /**
81 * Free a block allocated by this allocator. 63 * The allocator's free() implementation.
82 *
83 * \note Freeing a block of a different allocator is undefined.
84 *
85 * @param data the allocator's data
86 * @param mem a pointer to the block to free
87 */ 64 */
88 void (*free)(void *data, void *mem) 65 void (*free)(void *data, void *mem)
89 __attribute__((__nonnull__)); 66 __attribute__((__nonnull__));
90 } cx_allocator_class; 67 } cx_allocator_class;
91 68

mercurial