25 #ifndef DISABLE_MEMORY_SENTINELS
33 #define MEMAREA_ALIGN SIZEOF_VOID_P
37 #if MEMAREA_ALIGN == 4
38 #define MEMAREA_ALIGN_MASK ((uintptr_t)3)
39 #elif MEMAREA_ALIGN == 8
40 #define MEMAREA_ALIGN_MASK ((uintptr_t)7)
42 #error "void* is neither 4 nor 8 bytes long."
45 #if defined(__GNUC__) && defined(FLEXIBLE_ARRAY_MEMBER)
46 #define USE_ALIGNED_ATTRIBUTE
56 #define SENTINEL_VAL 0x90806622u
58 #define SENTINEL_LEN sizeof(uint32_t)
61 #define SET_SENTINEL(chunk) \
63 set_uint32( &(chunk)->U_MEM[chunk->mem_size], SENTINEL_VAL ); \
66 #define CHECK_SENTINEL(chunk) \
68 uint32_t sent_val = get_uint32(&(chunk)->U_MEM[chunk->mem_size]); \
69 tor_assert(sent_val == SENTINEL_VAL); \
72 #define SENTINEL_LEN 0
73 #define SET_SENTINEL(chunk) STMT_NIL
74 #define CHECK_SENTINEL(chunk) STMT_NIL
81 uintptr_t x = (uintptr_t)ptr;
82 x = (x+MEMAREA_ALIGN_MASK) & ~MEMAREA_ALIGN_MASK;
98 #ifdef USE_ALIGNED_ATTRIBUTE
112 #define CHUNK_HEADER_SIZE offsetof(memarea_chunk_t, U_MEM)
115 #define CHUNK_SIZE 4096
132 res = tor_malloc(chunk_size);
137 ((
char*)res)+chunk_size);
166 for (chunk = area->
first; chunk; chunk = next) {
199 if (ptr >= chunk->U_MEM && ptr < chunk->next_mem)
219 const size_t space_remaining =
221 if (sz > space_remaining) {
232 area->
first = chunk = new_chunk;
251 memset(result, 0, sz);
260 memcpy(result, s, n);
278 for (ln = 0; ln < n && s[ln]; ++ln)
281 memcpy(result, s, ln);
335 smartlist_free(area->pieces);
354 void *result = tor_malloc(sz);
362 void *result = tor_malloc_zero(sz);
376 size_t n = strlen(s);
385 size_t ln = strnlen(s, n);
393 size_t *allocated_out,
size_t *used_out)
396 *allocated_out = *used_out = 128;
Inline functions for reading and writing multibyte values from the middle of strings,...
void tor_free_(void *mem)
Headers for util_malloc.c.
#define SET_SENTINEL(chunk)
static memarea_chunk_t * alloc_chunk(size_t sz)
#define CHUNK_HEADER_SIZE
int memarea_owns_ptr(const memarea_t *area, const void *p)
void memarea_clear(memarea_t *area)
memarea_t * memarea_new(void)
void * memarea_memdup(memarea_t *area, const void *s, size_t n)
void memarea_get_stats(memarea_t *area, size_t *allocated_out, size_t *used_out)
void * memarea_alloc(memarea_t *area, size_t sz)
static void memarea_chunk_free_unchecked(memarea_chunk_t *chunk)
static void * realign_pointer(void *ptr)
void memarea_assert_ok(memarea_t *area)
void memarea_drop_all_(memarea_t *area)
char * memarea_strndup(memarea_t *area, const char *s, size_t n)
char * memarea_strdup(memarea_t *area, const char *s)
#define CHECK_SENTINEL(chunk)
void * memarea_alloc_zero(memarea_t *area, size_t sz)
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_clear(smartlist_t *sl)
Top-level declarations for the smartlist_t dynamic array type.
Macros for iterating over the elements of a smartlist_t.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
void * void_for_alignment_
struct memarea_chunk_t * next_chunk
union memarea_chunk_t::@26 u
Integer definitions used throughout Tor.
Macros to manage assertions, fatal and non-fatal.