OPIC
Object Persistence In C
Data Structures | Macros | Typedefs | Functions
Malloc

Data Structures

struct  OPHeap
 Memory allocator object with persistent storage on disk. More...
 

Macros

#define OPHEAP_BITS   36
 Size of OPHeap represented in bits.
 
#define OPHEAP_SIZE   (1UL << OPHEAP_BITS)
 Size of OPHeap.
 
#define OPLENREF_MAX_LEN   (1ULL << (64 - OPHEAP_BITS))
 Maximum size oplenref_t can hold. More...
 

Typedefs

typedef uintptr_t opref_t
 The "pointer type" used within objects created by OPHeap. More...
 
typedef uintptr_t oplenref_t
 Another "pointer type" used within objects created in OPHeap. More...
 

Functions

static opref_t OPPtr2Ref (void *addr)
 Converts a pointer allocated in OPHeap to an opref_t. More...
 
static void * OPRef2Ptr (void *ptr_in_heap, opref_t ref)
 Converts an opref_t reference to a regular pointer. More...
 
static size_t OPLenRef2Size (oplenref_t ref)
 Obtain the size encoded in oplenref_t. More...
 
static void * OPLenRef2Ptr (oplenref_t *ref, size_t container_size)
 Converts an oplenref_t reference to a regular pointer. More...
 
static void * OPLenRef2PtrOnHeap (oplenref_t *ref, size_t container_size, OPHeap *heap)
 Converts an oplenref_t reference to a regular pointer on specified heap. More...
 
static void OPLenRefCreate (oplenref_t *ref, void *data, size_t data_size, size_t container_size)
 A constructor for oplenref_t to hold the input data. More...
 
static void OPLenRefDelete (oplenref_t *ref, size_t container_size)
 A destructor for oplenref_t. More...
 
static void OPLenRefRelpace (oplenref_t *ref, void *data, size_t data_size, size_t container_size)
 Relace the data stored in oplenref_t. More...
 

Detailed Description

Macro Definition Documentation

◆ OPLENREF_MAX_LEN

#define OPLENREF_MAX_LEN   (1ULL << (64 - OPHEAP_BITS))

#include <opic/op_malloc.h>

Maximum size oplenref_t can hold.

The size is roughly 256MB, which should be sufficient for most applications.

Definition at line 143 of file op_malloc.h.

Typedef Documentation

◆ opref_t

#include <opic/op_malloc.h>

The "pointer type" used within objects created by OPHeap.

For all the objects relationship in OPHeap, user must use opref_t or oplenref_t instead of regular pointer. Regular pointers would be invalid when OPHeap is written to disk. To access the object referenced by opref_t, first dereference opref_t to regular pointer, then dereference the pointer.

Unfortunately, C type system is quite weak. It would be better if we have the following features:

  1. preserve the type information of the address we points to.
  2. operator overload, or makes every symbol posible to be used as an operator like what Haskell does. Common operation like dereferencing should have its own operator.

Here is an example of how to reference other objects using opref_t:

struct A {
opref_t ref_b;
};
struct B {
int x;
};
OPHeap* heap = OPHeapOpenTmp();
struct A* a = OPMalloc(heap, sizeof(struct A));
struct B* b = OPMalloc(heap, sizeof(struct B));
struct B* b2;
// We store the pointer as opref_t
a->ref_b = OPPtr2Ref(b);
// When accessing the memory, dereference it as a pointer
b2 = OPRef2Ptr(a, a->ref_b);
See also

Definition at line 109 of file op_malloc.h.

◆ oplenref_t

#include <opic/op_malloc.h>

Another "pointer type" used within objects created in OPHeap.

oplenref_t can encode an object created in OPHeap and the size of such object. User can query the size of the object by OPLenRef2Szie. The size of the object must smaller than OPLENREF_MAX_LEN, which is 256MB and suppose to be sufficient for regular use.

When oplenref_t is used in a container, oplenref_t can be used to encode the inline data in the container. The container holding oplenref_t can provide additional space next to oplenref_t to hold the inline data. When encoding or decoding oplenref_t the addtional space information must present so we know if the data is next to oplenref_t, or it is somewhere else in OPHeap.

See also

Definition at line 135 of file op_malloc.h.

Function Documentation

◆ OPPtr2Ref()

static opref_t OPPtr2Ref ( void *  addr)
inlinestatic

#include <opic/op_malloc.h>

Converts a pointer allocated in OPHeap to an opref_t.

Parameters
addrAny pointer that is allocated with OPHeap.
Returns
An opref_t value.

Definition at line 326 of file op_malloc.h.

◆ OPRef2Ptr()

static void* OPRef2Ptr ( void *  ptr_in_heap,
opref_t  ref 
)
inlinestatic

#include <opic/op_malloc.h>

Converts an opref_t reference to a regular pointer.

Parameters
ptr_in_heapAny pointer in the heap, including OPHeap*.
refA opref_t value.
Returns
A regular pointer.

Definition at line 340 of file op_malloc.h.

◆ OPLenRef2Size()

static size_t OPLenRef2Size ( oplenref_t  ref)
inlinestatic

#include <opic/op_malloc.h>

Obtain the size encoded in oplenref_t.

Parameters
refA oplenref_t value.
Returns
The size of the object it pointed to.

Definition at line 353 of file op_malloc.h.

◆ OPLenRef2Ptr()

static void* OPLenRef2Ptr ( oplenref_t ref,
size_t  container_size 
)
inlinestatic

#include <opic/op_malloc.h>

Converts an oplenref_t reference to a regular pointer.

Parameters
refPointer to oplenref_t.
container_sizeSize of the container that holds oplenref_t. If used without a container, set this parameter to 0.
Returns
A regular pointer.

Definition at line 405 of file op_malloc.h.

◆ OPLenRef2PtrOnHeap()

static void* OPLenRef2PtrOnHeap ( oplenref_t ref,
size_t  container_size,
OPHeap heap 
)
inlinestatic

#include <opic/op_malloc.h>

Converts an oplenref_t reference to a regular pointer on specified heap.

This method is useful when oplenref_t is not on OPHeap, therefore we need to assist with a specific heap.

Parameters
refPointer to oplenref_t.
container_sizeSize of the container that holds oplenref_t. If used without a container, set this parameter to 0.
heapAddress to OPHeap where the data pointer may locate at.
Returns
A regular pointer.

Definition at line 430 of file op_malloc.h.

◆ OPLenRefCreate()

static void OPLenRefCreate ( oplenref_t ref,
void *  data,
size_t  data_size,
size_t  container_size 
)
inlinestatic

#include <opic/op_malloc.h>

A constructor for oplenref_t to hold the input data.

When oplenref_t used in a container, there would be some spaces next to the oplenref_t address that can hold inline data. The inline space is specified by the container_size. Data get copied to the inline space if the data_size is smaller than the container_size; otherwise the data would get copied to a newly allocated object in OPHeap. In either case, oplenref_t would store the information of which scheme was used.

Parameters
refPointer to oplenref_t.
dataThe data to copy over to OPHeap.
data_sizeSize of the data. This size must smaller than OPLENREF_MAX_LEN.
container_sizeSize of the container that holds oplenref_t. If used without a container, set this paramter to 0.

Definition at line 461 of file op_malloc.h.

◆ OPLenRefDelete()

static void OPLenRefDelete ( oplenref_t ref,
size_t  container_size 
)
inlinestatic

#include <opic/op_malloc.h>

A destructor for oplenref_t.

Deallocates the data stored in oplenref_t.

Parameters
refPointer to oplenref_t.
container_sizeSize of the container that holds oplenref_t. If used without a container, set this paramter to 0.

Definition at line 502 of file op_malloc.h.

◆ OPLenRefRelpace()

static void OPLenRefRelpace ( oplenref_t ref,
void *  data,
size_t  data_size,
size_t  container_size 
)
inlinestatic

#include <opic/op_malloc.h>

Relace the data stored in oplenref_t.

Will deallocate the original data stored in oplenref_t if it exists.

Parameters
refPointer to oplenref_t.
dataThe data to copy over to OPHeap.
data_sizeSize of the data.
container_sizeSize of the container that holds oplenref_t. If used without a container, set this paramter to 0.

Definition at line 527 of file op_malloc.h.