Di_mem_chunk

## Data Structures

struct | di_mem_chunk
| a mem chunk More...
struct | di_mem_area
| a mem area More...

## Functions

di_mem_chunk * | di_mem_chunk_new (di_ksize_t atom_size, di_ksize_t area_size)
void * | di_mem_chunk_alloc (di_mem_chunk *mem_chunk)
void * | di_mem_chunk_alloc0 (di_mem_chunk *mem_chunk)
void | di_mem_chunk_destroy (di_mem_chunk *mem_chunk)
size_t | di_mem_chunk_size (di_mem_chunk *mem_chunk)
static size_t | internal_di_mem_chunk_compute_size (size_t size, size_t min_size)

## Detailed Description

## Function Documentation

## di_mem_chunk_alloc()

void * di_mem_chunk_alloc | ( | di_mem_chunk * | mem_chunk | ) |

Allocate a piece

Parameters

  mem_chunk | a di_mem_chunk

Returns
  memory

{
void *mem;
if ((!mem_chunk->mem_area) || ((mem_chunk->mem_area->index + mem_chunk->atom_size) > mem_chunk->area_size))
{
mem_chunk->mem_area = di_malloc (mem_chunk->rarea_size);
mem_chunk->num_mem_areas += 1;
mem_chunk->mem_area->next = mem_chunk->mem_areas;
mem_chunk->mem_area->prev = NULL;
if (mem_chunk->mem_areas)
mem_chunk->mem_areas->prev = mem_chunk->mem_area;
mem_chunk->mem_areas = mem_chunk->mem_area;
mem_chunk->mem_area->index = 0;
mem_chunk->mem_area->free = mem_chunk->area_size;
mem_chunk->mem_area->allocated = 0;
}
mem = &mem_chunk->mem_area->mem[mem_chunk->mem_area->index];
mem_chunk->mem_area->index += mem_chunk->atom_size;
mem_chunk->mem_area->free -= mem_chunk->atom_size;
mem_chunk->mem_area->allocated += 1;
return mem;
}
di_malloc
void * di_malloc(size_t n_bytes) __attribute__((malloc))
Definition mem.c:29

References di_malloc().

Referenced by di_mem_chunk_alloc0().

## di_mem_chunk_alloc0()

void * di_mem_chunk_alloc0 | ( | di_mem_chunk * | mem_chunk | ) |

Allocate a cleared piece

Parameters

  mem_chunk | a di_mem_chunk

Returns
  memory

{
void *mem;
mem = di_mem_chunk_alloc (mem_chunk);
if (mem)
memset (mem, 0, mem_chunk->atom_size);
return mem;
}
di_mem_chunk_alloc
void * di_mem_chunk_alloc(di_mem_chunk *mem_chunk)
Definition mem_chunk.c:120

References di_mem_chunk_alloc().

## di_mem_chunk_destroy()

void di_mem_chunk_destroy | ( | di_mem_chunk * | mem_chunk | ) |

{
di_mem_area *mem_areas, *temp_area;
mem_areas = mem_chunk->mem_areas;
while (mem_areas)
{
temp_area = mem_areas;
mem_areas = mem_areas->next;
di_free (temp_area);
}
di_free (mem_chunk);
}
di_free
void di_free(void *mem)
Definition mem.c:60
di_mem_area
a mem area
Definition mem_chunk.c:62
di_mem_area::next
di_mem_area * next
Definition mem_chunk.c:63

## di_mem_chunk_new()

di_mem_chunk * di_mem_chunk_new | ( | di_ksize_t | atom_size,
|  | di_ksize_t | area_size )

Makes a new Memory-Chunk Allocer

Parameters

  atom_size | size of each piece
  area_size | size of each alloced chunk

{
di_mem_chunk *mem_chunk;
if (area_size < atom_size)
return NULL;
area_size = (area_size + atom_size - 1) / atom_size;
area_size *= atom_size;
mem_chunk = di_new (di_mem_chunk, 1);
mem_chunk->num_mem_areas = 0;
mem_chunk->num_marked_areas = 0;
mem_chunk->mem_area = NULL;
mem_chunk->mem_areas = NULL;
mem_chunk->atom_size = atom_size;
if (mem_chunk->atom_size % MEM_ALIGN)
mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
mem_chunk->rarea_size = internal_di_mem_chunk_compute_size (area_size + sizeof (di_mem_area) - MEM_AREA_SIZE, atom_size + sizeof (di_mem_area) - MEM_AREA_SIZE);
mem_chunk->area_size = mem_chunk->rarea_size - (sizeof (di_mem_area) - MEM_AREA_SIZE);
return mem_chunk;
}
di_new
#define di_new(struct_type, n_structs)
Definition mem.h:73
di_mem_chunk
a mem chunk
Definition mem_chunk.c:47

References di_new.

Referenced by di_hash_table_new_full(), di_packages_allocator_alloc(), di_release_alloc(), and internal_di_packages_allocator_alloc().

## di_mem_chunk_size()

size_t di_mem_chunk_size | ( | di_mem_chunk * | mem_chunk | ) |

{
di_mem_area *mem_area;
size_t size = 0;
for (mem_area = mem_chunk->mem_areas; mem_area; mem_area = mem_area->next)
{
size += mem_chunk->atom_size * mem_area->allocated;
}
return size;
}
di_mem_area::allocated
size_t allocated
Definition mem_chunk.c:67

## internal_di_mem_chunk_compute_size()

size_t internal_di_mem_chunk_compute_size | ( | size_t | size,
|  | size_t | min_size )

static

{
size_t power_of_2;
size_t lower, upper;
power_of_2 = 16;
while (power_of_2 < size)
power_of_2 <<= 1;
lower = power_of_2 >> 1;
upper = power_of_2;
if (size - lower < upper - size && lower >= min_size)
return lower;
else
return upper;
}