Lumiera  0.pre.03
»edit your freedom«
safeclib.c File Reference

Go to the source code of this file.

Description

Implementation of error-safe wrappers for some notorious C-Lib functions.

Definition in file safeclib.c.

#include "lib/error.h"
#include "lib/safeclib.h"
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <nobug.h>

Enumerations

enum  lumiera_resource {
  LUMIERA_RESOURCE_MEMORY,
  LUMIERA_RESOURCE_END
}
 Resources known to the resource collector. More...
 
enum  lumiera_resource_try {
  LUMIERA_RESOURCE_NONE,
  LUMIERA_RESOURCE_ONE,
  LUMIERA_RESOURCE_SOME,
  LUMIERA_RESOURCE_MANY,
  LUMIERA_RESOURCE_ALL,
  LUMIERA_RESOURCE_PANIC,
  LUMIERA_RESOURCE_UNREGISTER
}
 Iteration indicator. More...
 

Functions

static int die_no_mem (enum lumiera_resource which, enum lumiera_resource_try *iteration, void *context)
 
void * lumiera_calloc (size_t n, size_t size)
 Allocate cleared memory for an array. More...
 
 LUMIERA_ERROR_DEFINE (NO_MEMORY, "Out of Memory!")
 
void * lumiera_malloc (size_t size)
 Allocate memory. More...
 
void * lumiera_realloc (void *ptr, size_t size)
 Change the size of a memory block. More...
 
int lumiera_streq (const char *a, const char *b)
 check 2 strings for identity. More...
 
int lumiera_strncmp (const char *a, const char *b, size_t len)
 Compare two C strings. More...
 
char * lumiera_strndup (const char *str, size_t len)
 Duplicate a C string. More...
 

Enumeration Type Documentation

◆ lumiera_resource

Resources known to the resource collector.

Todo:
10/2023 this is a left-over of the »resource-collector« plan
Enumerator
LUMIERA_RESOURCE_MEMORY 

memory blocks, context is a pointer to the size_t required

LUMIERA_RESOURCE_END 

OS filehandles.

CPU time, as in threads and such mmaped regions disk space for the storage area, context is a pointer to the filename indication the device disk bandwidth for the storage area, context is a pointer to the filename indication the device disk space for the caching area, context is a pointer to the filename indication the device disk bandwidth for the caching area, context is a pointer to the filename indication the device

Definition at line 46 of file safeclib.c.

◆ lumiera_resource_try

Iteration indicator.

Todo:
10/2023 this is a left-over of the »resource-collector« plan Resource collection works iteratively freeing more and more resources.

Handlers do not need to obey the request and shall return LUMIERA_RESOURCE_NONE which will then continue with the next handler. This goes through all available handlers until one returns a higher or same value than the current iteration to indicate that it freed enough resources to continue the task. Then control is passed back to the calling loop which retries the resource allocation. LUMIERA_RESOURCE_PANIC is somewhat special since it will always call all registered handlers for all resources, not only the queried one and finally _exit() the application. The exact amounts of resources to be freed for ONE, SOME and MANY in intentionally kept vague the handlers are free to interpret this in some sensible way.

Enumerator
LUMIERA_RESOURCE_NONE 

No op, returned by a handler when it did nothing.

LUMIERA_RESOURCE_ONE 

try to free one or really few of this resources

LUMIERA_RESOURCE_SOME 

try to free a small reasonable implementation defined amount of resources

LUMIERA_RESOURCE_MANY 

try to free a bigger implementation defined amount of resources

LUMIERA_RESOURCE_ALL 

free as much as possible

LUMIERA_RESOURCE_PANIC 

die!

LUMIERA_RESOURCE_UNREGISTER 

When a handler gets unregistered it will be called with this value to give it a chance to clean up the user 'data'.

Definition at line 83 of file safeclib.c.

Function Documentation

◆ die_no_mem()

static int die_no_mem ( enum lumiera_resource  which,
enum lumiera_resource_try iteration,
void *  context 
)
static

placeholder function in case the resource-collector was not installed

Todo:
10/2023 in the early stages of the Lumiera project a centralised resource pooling was considered. On resource shortage, the (planned) resource-collector would attempt to reclaim excess resources. This would allow to use resources (e.g. memory) wastefully as trade-off for performance gains. This concept was never developed beyond an interface draft; most notably it was never clarified how excess resources could have been identified on-demand while in-flight. Today I view this concept as an immature plan and remove the remaining draft code.

Definition at line 113 of file safeclib.c.

References LUMIERA_DIE.

◆ lumiera_malloc()

void* lumiera_malloc ( size_t  sz)

Allocate memory.

always succeeds or dies

Parameters
sizememory to be allocated
Returns
pointer to the allocated memory

Definition at line 122 of file safeclib.c.

References LUMIERA_RESOURCE_ONE.

Referenced by lumiera_plugin_new().

+ Here is the caller graph for this function:

◆ lumiera_calloc()

void* lumiera_calloc ( size_t  n,
size_t  size 
)

Allocate cleared memory for an array.

always succeeds or dies

Parameters
nnumber of elements
sizememory to be allocated
Returns
pointer to the allocated memory

Definition at line 139 of file safeclib.c.

References LUMIERA_RESOURCE_ONE.

◆ lumiera_realloc()

void* lumiera_realloc ( void *  ptr,
size_t  size 
)

Change the size of a memory block.

Parameters
ptrpointer to the old memory block obtained by lumiera_malloc or lumiera_calloc
sizenew size of the block
Returns
address of new block

Definition at line 158 of file safeclib.c.

References LUMIERA_RESOURCE_ONE.

◆ lumiera_strndup()

char* lumiera_strndup ( const char *  str,
size_t  len 
)

Duplicate a C string.

always succeeds or dies

Parameters
strstring to be copied
lenmaximal length to be copied
Returns
pointer to the new string, "" if NULL was passed as str

Definition at line 175 of file safeclib.c.

References LUMIERA_RESOURCE_ONE.

Referenced by lumiera_free(), and lumiera_plugin_new().

+ Here is the caller graph for this function:

◆ lumiera_strncmp()

int lumiera_strncmp ( const char *  a,
const char *  b,
size_t  len 
)

Compare two C strings.

Handles NULL pointers as "", shortcut for same addresses

Parameters
afirst string for comparsion
bsecond string for comparsion
lenmaximal length for the comparsion
Returns
0 if the strings are identical, -1 if smaller 1 if bigger.

Definition at line 193 of file safeclib.c.

Referenced by lumiera_free(), and lumiera_streq().

+ Here is the caller graph for this function:

◆ lumiera_streq()

int lumiera_streq ( const char *  a,
const char *  b 
)

check 2 strings for identity.

Parameters
afirst string for comparsion
bsecond string for comparsion
Returns
1 when the strings are the the same, 0 if not.

Definition at line 200 of file safeclib.c.

References lumiera_strncmp().

Referenced by lumiera_free().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: