Back to index

tor  0.2.3.19-rc
Typedefs | Functions
memarea.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct memarea_t

Functions

memarea_tmemarea_new (void)
 Allocate and return new memarea.
void memarea_drop_all (memarea_t *area)
 Free area, invalidating all pointers returned from memarea_alloc() and friends for this area.
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.
int memarea_owns_ptr (const memarea_t *area, const void *ptr)
 Return true iff p is in a range that has been returned by an allocation from area.
void * memarea_alloc (memarea_t *area, size_t sz)
 Return a pointer to a chunk of memory in area of at least sz bytes.
void * memarea_alloc_zero (memarea_t *area, size_t sz)
 As memarea_alloc(), but clears the memory it returns.
void * memarea_memdup (memarea_t *area, const void *s, size_t n)
 As memdup, but returns the memory 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.
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_clear_freelist (void)
 Remove all unused memarea chunks from the internal freelist.
void memarea_assert_ok (memarea_t *area)
 Assert that area is okay.

Typedef Documentation

typedef struct memarea_t

Definition at line 8 of file memarea.h.


Function Documentation

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 220 of file memarea.c.

{
  memarea_chunk_t *chunk = area->first;
  char *result;
  tor_assert(chunk);
  CHECK_SENTINEL(chunk);
  tor_assert(sz < SIZE_T_CEILING);
  if (sz == 0)
    sz = 1;
  if (chunk->next_mem+sz > chunk->u.mem+chunk->mem_size) {
    if (sz+CHUNK_HEADER_SIZE >= CHUNK_SIZE) {
      /* This allocation is too big.  Stick it in a special chunk, and put
       * that chunk second in the list. */
      memarea_chunk_t *new_chunk = alloc_chunk(sz+CHUNK_HEADER_SIZE, 0);
      new_chunk->next_chunk = chunk->next_chunk;
      chunk->next_chunk = new_chunk;
      chunk = new_chunk;
    } else {
      memarea_chunk_t *new_chunk = alloc_chunk(CHUNK_SIZE, 1);
      new_chunk->next_chunk = chunk;
      area->first = chunk = new_chunk;
    }
    tor_assert(chunk->mem_size >= sz);
  }
  result = chunk->next_mem;
  chunk->next_mem = chunk->next_mem + sz;
  /* Reinstate these if bug 930 ever comes back
  tor_assert(chunk->next_mem >= chunk->u.mem);
  tor_assert(chunk->next_mem <= chunk->u.mem+chunk->mem_size);
  */
  chunk->next_mem = realign_pointer(chunk->next_mem);
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* memarea_alloc_zero ( memarea_t area,
size_t  sz 
)

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

Definition at line 256 of file memarea.c.

{
  void *result = memarea_alloc(area, sz);
  memset(result, 0, sz);
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void memarea_assert_ok ( memarea_t area)

Assert that area is okay.

Definition at line 316 of file memarea.c.

{
  memarea_chunk_t *chunk;
  tor_assert(area->first);

  for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
    CHECK_SENTINEL(chunk);
    tor_assert(chunk->next_mem >= chunk->u.mem);
    tor_assert(chunk->next_mem <=
          (char*) realign_pointer(chunk->u.mem+chunk->mem_size));
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

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 176 of file memarea.c.

{
  memarea_chunk_t *chunk, *next;
  if (area->first->next_chunk) {
    for (chunk = area->first->next_chunk; chunk; chunk = next) {
      next = chunk->next_chunk;
      chunk_free_unchecked(chunk);
    }
    area->first->next_chunk = NULL;
  }
  area->first->next_mem = area->first->u.mem;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void memarea_clear_freelist ( void  )

Remove all unused memarea chunks from the internal freelist.

Definition at line 191 of file memarea.c.

{
  memarea_chunk_t *chunk, *next;
  freelist_len = 0;
  for (chunk = freelist; chunk; chunk = next) {
    next = chunk->next_chunk;
    tor_free(chunk);
  }
  freelist = NULL;
}

Here is the caller graph for this function:

void memarea_drop_all ( memarea_t area)

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

Definition at line 161 of file memarea.c.

{
  memarea_chunk_t *chunk, *next;
  for (chunk = area->first; chunk; chunk = next) {
    next = chunk->next_chunk;
    chunk_free_unchecked(chunk);
  }
  area->first = NULL; /*fail fast on */
  tor_free(area);
}

Here is the call graph for this function:

Here is the caller graph for this function:

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 300 of file memarea.c.

{
  size_t a = 0, u = 0;
  memarea_chunk_t *chunk;
  for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
    CHECK_SENTINEL(chunk);
    a += CHUNK_HEADER_SIZE + chunk->mem_size;
    tor_assert(chunk->next_mem >= chunk->u.mem);
    u += CHUNK_HEADER_SIZE + (chunk->next_mem - chunk->u.mem);
  }
  *allocated_out = a;
  *used_out = u;
}
void* memarea_memdup ( memarea_t area,
const void *  s,
size_t  n 
)

As memdup, but returns the memory from area.

Definition at line 265 of file memarea.c.

{
  char *result = memarea_alloc(area, n);
  memcpy(result, s, n);
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

memarea_t* memarea_new ( void  )

Allocate and return new memarea.

Definition at line 151 of file memarea.c.

{
  memarea_t *head = tor_malloc(sizeof(memarea_t));
  head->first = alloc_chunk(CHUNK_SIZE, 1);
  return head;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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 205 of file memarea.c.

{
  memarea_chunk_t *chunk;
  const char *ptr = p;
  for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
    if (ptr >= chunk->u.mem && ptr < chunk->next_mem)
      return 1;
  }
  return 0;
}

Here is the caller graph for this function:

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

As strdup, but returns the memory from area.

Definition at line 274 of file memarea.c.

{
  return memarea_memdup(area, s, strlen(s)+1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

As strndup, but returns the memory from area.

Definition at line 281 of file memarea.c.

{
  size_t ln;
  char *result;
  const char *cp, *end = s+n;
  tor_assert(n < SIZE_T_CEILING);
  for (cp = s; cp < end && *cp; ++cp)
    ;
  /* cp now points to s+n, or to the 0 in the string. */
  ln = cp-s;
  result = memarea_alloc(area, ln+1);
  memcpy(result, s, ln);
  result[ln]='\0';
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function: