ssm  0.0.2
Runtime Library for the Sparse Synchronous Model
ssm-mem.c File Reference

SSM runtime memory management and allocation. More...

#include <assert.h>
#include <ssm-internal.h>
#include <ssm.h>
#include <stdio.h>
#include <stdlib.h>
+ Include dependency graph for ssm-mem.c:

Go to the source code of this file.

Data Structures

union  block
 (The beginning of) a block of memory. More...
 
struct  mem_pool
 A memory pool, which maintains a free list. More...
 

Macros

#define BLOCKS_PER_PAGE   (SSM_MEM_PAGE_SIZE / sizeof(block_t))
 The number of blocks in each memory page. More...
 
#define UNINITIALIZED_FREE_BLOCK   ((block_t *)0x0)
 A "pointer" that points to the next contiguous block in memory. More...
 
#define END_OF_FREELIST   ((block_t *)0x42)
 Sentinel value indicating the end of the free list. More...
 

Typedefs

typedef union block block_t
 (The beginning of) a block of memory. More...
 

Functions

static size_t find_pool_size (size_t size)
 Find the memory pool for some arbitrary size. More...
 
static block_tfind_next_block (block_t *block, size_t pool_size)
 
void ssm_mem_init (void *(*alloc_page_handler)(void), void *(*alloc_mem_handler)(size_t), void(*free_mem_handler)(void *, size_t))
 Initializes the underlying allocator system. More...
 
void ssm_mem_destroy (void(*free_page_handler)(void *))
 Tears down the underlying allocator system. More...
 
void * ssm_mem_alloc (size_t size)
 Allocate a contiguous range of memory. More...
 
void ssm_mem_free (void *m, size_t size)
 Deallocate memory allocated by ssm_mem_alloc(). More...
 
ssm_value_t ssm_new_time_int (ssm_time_t time)
 Allocate a ssm_time on the heap. More...
 
ssm_value_t ssm_new_adt_int (uint8_t field_count, uint8_t tag)
 Allocate a new ADT object on the heap. More...
 
ssm_value_t ssm_new_sv_int (ssm_value_t val)
 Allocate an ssm_sv on the heap. More...
 
ssm_value_t ssm_new_closure_int (ssm_func_t f, uint8_t arg_cap)
 Allocate a closure on the heap. More...
 
ssm_value_t ssm_new_array_int (uint16_t elems)
 Allocate an array on the heap. More...
 
ssm_value_t ssm_new_blob_int (uint16_t size)
 Allocate a blob on the heap. More...
 
void ssm_drop_final (ssm_value_t v)
 Finalize and free a heap object. More...
 
void ssm_dups (size_t cnt, ssm_value_t *arr)
 Call ssm_dup() on an array of values. More...
 
void ssm_drops (size_t cnt, ssm_value_t *arr)
 Call ssm_drop() on an array of values. More...
 
void ssm_mem_statistics_collect (ssm_mem_statistics_t *stats)
 Collect and return statistics about the heap. More...
 

Variables

struct mem_pool mem_pools [SSM_MEM_POOL_COUNT]
 Memory pools from SSM_MEM_POOL_MIN to SSM_MEM_POOL_MAX. More...
 
static void *(* alloc_page )(void)
 Page allocation handler, set by ssm_mem_init(). More...
 
static void *(* alloc_mem )(size_t size)
 Large memory allocation handler, set by ssm_mem_init(). More...
 
static void(* free_mem )(void *mem, size_t size)
 Large memory release handler, set by ssm_mem_init(). More...
 
static size_t num_pages_allocated = 0
 Tracks how many pages have been allocated by alloc_pool(). More...
 
static size_t live_objects = 0
 
static size_t pages_allocated [SSM_MEM_POOL_COUNT] = {0}
 
static size_t objects_allocated = 0
 
static size_t objects_freed = 0
 

Detailed Description

SSM runtime memory management and allocation.

Author
John Hui (j-hui)
Stephen Edwards (sedwards-lab)
Daniel Scanteianu (Scanteianu)

Definition in file ssm-mem.c.

Macro Definition Documentation

◆ BLOCKS_PER_PAGE

#define BLOCKS_PER_PAGE   (SSM_MEM_PAGE_SIZE / sizeof(block_t))

The number of blocks in each memory page.

Definition at line 34 of file ssm-mem.c.

◆ UNINITIALIZED_FREE_BLOCK

#define UNINITIALIZED_FREE_BLOCK   ((block_t *)0x0)

A "pointer" that points to the next contiguous block in memory.

Definition at line 37 of file ssm-mem.c.

◆ END_OF_FREELIST

#define END_OF_FREELIST   ((block_t *)0x42)

Sentinel value indicating the end of the free list.

Definition at line 40 of file ssm-mem.c.

Typedef Documentation

◆ block_t

typedef union block block_t

(The beginning of) a block of memory.

The size of each block varies depending on which memory pool it resides in. When a block is being used (allocated), block_buf effectively gives a pointer to the beginning that block that can be indexed into.

When a block is not being used, it is maintained in a free list. If it is not at the head of the free list, then it is pointed to by the free_list_next field of some other block_t, while its own free_list_next points to the next block in the free list. To avoid initializing the free_list_next fields of "fresh" blocks, when free_list_next is UNINITIALIZED_FREE_BLOCK, the next free block contiguously follows the current block in memory. The last free block points to END_OF_FREELIST.

Function Documentation

◆ find_pool_size()

static size_t find_pool_size ( size_t  size)
inlinestatic

Find the memory pool for some arbitrary size.

Tries to find the smallest memory pool that will fit a block of size. Returns SSM_MEM_POOL_COUNT if no such pool exists, i.e., if size is greater than SSM_MEM_POOL_MAX.

Since SSM_MEM_POOL_COUNT is a (small) compile-time constant, this linear search is effectively contant time.

Parameters
sizethe block size whose memory pool we are looking for.
Returns
index to the memory pool, or SSM_MEM_POOL_COUNT otherwise.

Definition at line 75 of file ssm-mem.c.

◆ find_next_block()

static block_t* find_next_block ( block_t block,
size_t  pool_size 
)
inlinestatic

Definition at line 82 of file ssm-mem.c.

Variable Documentation

◆ mem_pools

struct mem_pool mem_pools[SSM_MEM_POOL_COUNT]

Memory pools from SSM_MEM_POOL_MIN to SSM_MEM_POOL_MAX.

Definition at line 1 of file ssm-mem.c.

◆ alloc_page

void*(* alloc_page) (void) ( void  )
static

Page allocation handler, set by ssm_mem_init().

Definition at line 55 of file ssm-mem.c.

◆ alloc_mem

void*(* alloc_mem) (size_t size) ( size_t  size)
static

Large memory allocation handler, set by ssm_mem_init().

Definition at line 58 of file ssm-mem.c.

◆ free_mem

void(* free_mem) (void *mem, size_t size) ( void *  mem,
size_t  size 
)
static

Large memory release handler, set by ssm_mem_init().

Definition at line 61 of file ssm-mem.c.

◆ num_pages_allocated

size_t num_pages_allocated = 0
static

Tracks how many pages have been allocated by alloc_pool().

Note
Define CONFIG_MEM_STATS to enable this.

Definition at line 94 of file ssm-mem.c.

◆ live_objects

size_t live_objects = 0
static

Definition at line 95 of file ssm-mem.c.

◆ pages_allocated

size_t pages_allocated[SSM_MEM_POOL_COUNT] = {0}
static

Definition at line 96 of file ssm-mem.c.

◆ objects_allocated

size_t objects_allocated = 0
static

Definition at line 97 of file ssm-mem.c.

◆ objects_freed

size_t objects_freed = 0
static

Definition at line 98 of file ssm-mem.c.