Tor  0.4.7.0-alpha-dev
Data Structures | Macros | Functions
memarea.c File Reference

Implementation for memarea_t, an allocator for allocating lots of small objects that will be freed all at once. More...

#include "orconfig.h"
#include "lib/memarea/memarea.h"
#include <stdlib.h>
#include <string.h>
#include "lib/arch/bytes.h"
#include "lib/cc/torint.h"
#include "lib/smartlist_core/smartlist_core.h"
#include "lib/smartlist_core/smartlist_foreach.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"

Go to the source code of this file.

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
 

Functions

static void * realign_pointer (void *ptr)
 
static memarea_chunk_talloc_chunk (size_t sz)
 
static void memarea_chunk_free_unchecked (memarea_chunk_t *chunk)
 
memarea_tmemarea_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)
 

Detailed Description

Implementation for memarea_t, an allocator for allocating lots of small objects that will be freed all at once.

Definition in file memarea.c.

Macro Definition Documentation

◆ CHECK_SENTINEL

#define CHECK_SENTINEL (   chunk)
Value:
STMT_BEGIN \
uint32_t sent_val = get_uint32(&(chunk)->U_MEM[chunk->mem_size]); \
tor_assert(sent_val == SENTINEL_VAL); \
STMT_END
static uint32_t get_uint32(const void *cp)
Definition: bytes.h:54
#define SENTINEL_VAL
Definition: memarea.c:56
#define U_MEM
Definition: memarea.c:50

Assert that the sentinel on a memarea is set correctly.

Definition at line 66 of file memarea.c.

◆ CHUNK_HEADER_SIZE

#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?

Definition at line 112 of file memarea.c.

◆ CHUNK_SIZE

#define CHUNK_SIZE   4096

What's the smallest that we'll allocate a chunk?

Definition at line 115 of file memarea.c.

◆ MEMAREA_ALIGN

#define MEMAREA_ALIGN   SIZEOF_VOID_P

All returned pointers should be aligned to the nearest multiple of this value.

Definition at line 33 of file memarea.c.

◆ SENTINEL_LEN

#define SENTINEL_LEN   sizeof(uint32_t)

How many bytes per area do we devote to the sentinel?

Definition at line 58 of file memarea.c.

◆ SENTINEL_VAL

#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.

Definition at line 56 of file memarea.c.

◆ SET_SENTINEL

#define SET_SENTINEL (   chunk)
Value:
set_uint32( &(chunk)->U_MEM[chunk->mem_size], SENTINEL_VAL ); \
STMT_END
static void set_uint32(void *cp, uint32_t v)
Definition: bytes.h:87

Given a mem_area_chunk_t with SENTINEL_LEN extra bytes allocated at the end, set those bytes.

Definition at line 61 of file memarea.c.

◆ U_MEM

#define U_MEM   u.mem

A value which, when masked out of a pointer, produces a maximally aligned pointer.

Definition at line 50 of file memarea.c.

◆ USE_SENTINELS

#define USE_SENTINELS

If true, we try to detect any attempts to write beyond the length of a memarea.

Definition at line 29 of file memarea.c.

Function Documentation

◆ alloc_chunk()

static memarea_chunk_t* alloc_chunk ( size_t  sz)
static

Helper: allocate a new memarea chunk of around chunk_size bytes.

Definition at line 125 of file memarea.c.

Referenced by memarea_alloc().

◆ memarea_alloc()

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.

Definition at line 209 of file memarea.c.

Referenced by cdline_linecpy(), consensus_split_lines(), memarea_alloc_zero(), memarea_memdup(), memarea_strndup(), and storage_dir_save_labeled_to_file().

◆ memarea_alloc_zero()

void* memarea_alloc_zero ( memarea_t area,
size_t  sz 
)

As memarea_alloc(), but clears the memory it returns.

Definition at line 248 of file memarea.c.

◆ memarea_assert_ok()

void memarea_assert_ok ( memarea_t area)

Assert that area is okay.

Definition at line 305 of file memarea.c.

◆ memarea_chunk_free_unchecked()

static void memarea_chunk_free_unchecked ( memarea_chunk_t chunk)
static

Release chunk from a memarea.

Definition at line 145 of file memarea.c.

Referenced by memarea_clear(), and memarea_drop_all_().

◆ memarea_clear()

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.

Definition at line 178 of file memarea.c.

◆ memarea_drop_all_()

void memarea_drop_all_ ( memarea_t area)

Free area, invalidating all pointers returned from memarea_alloc() and friends for this area

Definition at line 163 of file memarea.c.

◆ memarea_get_stats()

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.

Definition at line 289 of file memarea.c.

◆ memarea_memdup()

void* memarea_memdup ( memarea_t area,
const void *  s,
size_t  n 
)

As memdup, but returns the memory from area.

Definition at line 257 of file memarea.c.

Referenced by cdline_linecpy(), and memarea_strdup().

◆ memarea_new()

memarea_t* memarea_new ( void  )

◆ memarea_owns_ptr()

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.

Definition at line 194 of file memarea.c.

◆ memarea_strdup()

char* memarea_strdup ( memarea_t area,
const char *  s 
)

As strdup, but returns the memory from area.

Definition at line 266 of file memarea.c.

◆ memarea_strndup()

char* memarea_strndup ( memarea_t area,
const char *  s,
size_t  n 
)

As strndup, but returns the memory from area.

Definition at line 273 of file memarea.c.

Referenced by get_token_arguments().

◆ realign_pointer()

static void* realign_pointer ( void *  ptr)
inlinestatic

Increment ptr until it is aligned to MEMAREA_ALIGN.

Definition at line 79 of file memarea.c.

Referenced by memarea_alloc(), and memarea_assert_ok().