tor
master
|
Implementation for memarea_t, an allocator for allocating lots of small objects that will be freed all at once. More...
#include "orconfig.h"
#include <stddef.h>
#include <stdlib.h>
#include "memarea.h"
#include "util.h"
#include "compat.h"
#include "torlog.h"
#include "container.h"
Data Structures | |
struct | memarea_chunk_t |
struct | memarea_t |
Macros | |
#define | USE_SENTINELS |
#define | MEMAREA_ALIGN SIZEOF_VOID_P |
#define | U_MEM u.mem |
#define | SENTINEL_VAL 0x90806622u |
#define | SENTINEL_LEN sizeof(uint32_t) |
#define | SET_SENTINEL(chunk) |
#define | CHECK_SENTINEL(chunk) |
#define | CHUNK_HEADER_SIZE offsetof(memarea_chunk_t, U_MEM) |
#define | CHUNK_SIZE 4096 |
Typedefs | |
typedef struct memarea_chunk_t | memarea_chunk_t |
Functions | |
memarea_t * | memarea_new (void) |
void | memarea_drop_all_ (memarea_t *area) |
void | memarea_clear (memarea_t *area) |
int | memarea_owns_ptr (const memarea_t *area, const void *p) |
void * | memarea_alloc (memarea_t *area, size_t sz) |
void * | memarea_alloc_zero (memarea_t *area, size_t sz) |
void * | memarea_memdup (memarea_t *area, const void *s, size_t n) |
char * | memarea_strdup (memarea_t *area, const char *s) |
char * | memarea_strndup (memarea_t *area, const char *s, size_t n) |
void | memarea_get_stats (memarea_t *area, size_t *allocated_out, size_t *used_out) |
void | memarea_assert_ok (memarea_t *area) |
Implementation for memarea_t, an allocator for allocating lots of small objects that will be freed all at once.
#define CHECK_SENTINEL | ( | chunk | ) |
Assert that the sentinel on a memarea is set correctly.
#define CHUNK_HEADER_SIZE offsetof(memarea_chunk_t, U_MEM) |
How many bytes are needed for overhead before we get to the memory part of a chunk?
#define CHUNK_SIZE 4096 |
What's the smallest that we'll allocate a chunk?
#define MEMAREA_ALIGN SIZEOF_VOID_P |
All returned pointers should be aligned to the nearest multiple of this value.
#define SENTINEL_LEN sizeof(uint32_t) |
How many bytes per area do we devote to the sentinel?
#define SENTINEL_VAL 0x90806622u |
Magic value that we stick at the end of a memarea so we can make sure there are no run-off-the-end bugs.
#define SET_SENTINEL | ( | chunk | ) |
Given a mem_area_chunk_t with SENTINEL_LEN extra bytes allocated at the end, set those bytes.
#define U_MEM u.mem |
A value which, when masked out of a pointer, produces a maximally aligned pointer.
#define USE_SENTINELS |
If true, we try to detect any attempts to write beyond the length of a memarea.
typedef struct memarea_chunk_t memarea_chunk_t |
Implements part of a memarea. New memory is carved off from chunk->mem in increasing order until a request is too big, at which point a new chunk is allocated.
void* memarea_alloc | ( | memarea_t * | area, |
size_t | sz | ||
) |
Return a pointer to a chunk of memory in area of at least sz bytes. sz should be significantly smaller than the area's chunk size, though we can deal if it isn't.
void* memarea_alloc_zero | ( | memarea_t * | area, |
size_t | sz | ||
) |
void memarea_assert_ok | ( | memarea_t * | area | ) |
Assert that area is okay.
void memarea_clear | ( | memarea_t * | area | ) |
Forget about having allocated anything in area, and free some of the backing storage associated with it, as appropriate. Invalidates all pointers returned from memarea_alloc() for this area.
void memarea_drop_all_ | ( | memarea_t * | area | ) |
Free area, invalidating all pointers returned from memarea_alloc() and friends for this area
void memarea_get_stats | ( | memarea_t * | area, |
size_t * | allocated_out, | ||
size_t * | used_out | ||
) |
Set allocated_out to the number of bytes allocated in area, and used_out to the number of bytes currently used.
void* memarea_memdup | ( | memarea_t * | area, |
const void * | s, | ||
size_t | n | ||
) |
As memdup, but returns the memory from area.
memarea_t* memarea_new | ( | void | ) |
Allocate and return new memarea.
int memarea_owns_ptr | ( | const memarea_t * | area, |
const void * | p | ||
) |
Return true iff p is in a range that has been returned by an allocation from area.
char* memarea_strdup | ( | memarea_t * | area, |
const char * | s | ||
) |
As strdup, but returns the memory from area.
char* memarea_strndup | ( | memarea_t * | area, |
const char * | s, | ||
size_t | n | ||
) |
As strndup, but returns the memory from area.