hook the resourcecollector into safeclib on backend startup
This commit is contained in:
parent
d103346482
commit
80e2db4800
4 changed files with 92 additions and 16 deletions
|
|
@ -89,6 +89,10 @@ lumiera_backend_init (void)
|
||||||
mpool_init_hook = lumiera_backend_resourcecollector_register_mpool;
|
mpool_init_hook = lumiera_backend_resourcecollector_register_mpool;
|
||||||
mpool_destroy_hook = lumiera_backend_resourcecollector_unregister_mpool;
|
mpool_destroy_hook = lumiera_backend_resourcecollector_unregister_mpool;
|
||||||
|
|
||||||
|
/* hook the resourcecollector into the safeclib allocation functions */
|
||||||
|
lumiera_safeclib_set_resourcecollector (lumiera_resourcecollector_run);
|
||||||
|
|
||||||
|
PLANNED("The resourcecollector aborts by default when there is no final strategy for recovery, TODO: initiate sane shutdown");
|
||||||
|
|
||||||
lumiera_threadpool_init ();
|
lumiera_threadpool_init ();
|
||||||
PLANNED ("hook threadpool into the resourcecollector (maybe in threadpool_init() instead here");
|
PLANNED ("hook threadpool into the resourcecollector (maybe in threadpool_init() instead here");
|
||||||
|
|
@ -144,6 +148,13 @@ lumiera_backend_destroy (void)
|
||||||
lumiera_filedescriptorregistry_destroy ();
|
lumiera_filedescriptorregistry_destroy ();
|
||||||
lumiera_threadpool_destroy ();
|
lumiera_threadpool_destroy ();
|
||||||
|
|
||||||
|
lumiera_safeclib_set_resourcecollector (NULL);
|
||||||
|
|
||||||
|
mpool_init_hook = NULL;
|
||||||
|
mpool_destroy_hook = NULL;
|
||||||
|
mpool_malloc_hook = malloc;
|
||||||
|
mpool_free_hook = free;
|
||||||
|
|
||||||
lumiera_resourcecollector_destroy ();
|
lumiera_resourcecollector_destroy ();
|
||||||
|
|
||||||
lumiera_mutex_destroy (&lumiera_filecreate_mutex, &NOBUG_FLAG (mutex_dbg), NOBUG_CONTEXT);
|
lumiera_mutex_destroy (&lumiera_filecreate_mutex, &NOBUG_FLAG (mutex_dbg), NOBUG_CONTEXT);
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,7 @@ enum lumiera_resource_try
|
||||||
* @return indication what the the handler really did (LUMIERA_RESOURCE_NONE when it didn't obey the request)
|
* @return indication what the the handler really did (LUMIERA_RESOURCE_NONE when it didn't obey the request)
|
||||||
*/
|
*/
|
||||||
typedef enum lumiera_resource_try (*lumiera_resource_handler_fn)(enum lumiera_resource_try itr, void* data, void* context);
|
typedef enum lumiera_resource_try (*lumiera_resource_handler_fn)(enum lumiera_resource_try itr, void* data, void* context);
|
||||||
|
typedef int (*lumiera_resourcecollector_run_fn) (enum lumiera_resource which, enum lumiera_resource_try* iteration, void* context);
|
||||||
|
|
||||||
typedef struct lumiera_resourcehandler_struct lumiera_resourcehandler;
|
typedef struct lumiera_resourcehandler_struct lumiera_resourcehandler;
|
||||||
typedef lumiera_resourcehandler* LumieraResourcehandler;
|
typedef lumiera_resourcehandler* LumieraResourcehandler;
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@
|
||||||
#include "lib/error.h"
|
#include "lib/error.h"
|
||||||
#include "lib/safeclib.h"
|
#include "lib/safeclib.h"
|
||||||
|
|
||||||
|
#include "backend/resourcecollector.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
@ -31,12 +33,40 @@
|
||||||
|
|
||||||
LUMIERA_ERROR_DEFINE (NO_MEMORY, "Out of Memory!");
|
LUMIERA_ERROR_DEFINE (NO_MEMORY, "Out of Memory!");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* placeholder function until the resourcecollector gets hooked in */
|
||||||
|
static int
|
||||||
|
die_no_mem (enum lumiera_resource which, enum lumiera_resource_try* iteration, void* context)
|
||||||
|
{
|
||||||
|
(void) which; (void) iteration; (void) context;
|
||||||
|
LUMIERA_DIE (NO_MEMORY);
|
||||||
|
return 0; /* not reached */
|
||||||
|
}
|
||||||
|
|
||||||
|
static lumiera_resourcecollector_run_fn lumiera_safeclib_resourcecollector_run_hook = die_no_mem;
|
||||||
|
|
||||||
|
void
|
||||||
|
lumiera_safeclib_set_resourcecollector (void* hook)
|
||||||
|
{
|
||||||
|
if (hook)
|
||||||
|
lumiera_safeclib_resourcecollector_run_hook = (lumiera_resourcecollector_run_fn)hook;
|
||||||
|
else
|
||||||
|
lumiera_safeclib_resourcecollector_run_hook = die_no_mem;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void*
|
void*
|
||||||
lumiera_malloc (size_t size)
|
lumiera_malloc (size_t size)
|
||||||
{
|
{
|
||||||
void* o = size ? malloc (size) : NULL;
|
enum lumiera_resource_try iteration = LUMIERA_RESOURCE_ONE;
|
||||||
if (!o)
|
void* o = NULL;
|
||||||
LUMIERA_DIE (NO_MEMORY);
|
|
||||||
|
if (size)
|
||||||
|
do
|
||||||
|
o = malloc (size);
|
||||||
|
while (!o && lumiera_safeclib_resourcecollector_run_hook (LUMIERA_RESOURCE_MEMORY, &iteration, &size));
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,9 +74,16 @@ lumiera_malloc (size_t size)
|
||||||
void*
|
void*
|
||||||
lumiera_calloc (size_t n, size_t size)
|
lumiera_calloc (size_t n, size_t size)
|
||||||
{
|
{
|
||||||
void* o = (n&&size)? calloc (n, size) : NULL;
|
enum lumiera_resource_try iteration = LUMIERA_RESOURCE_ONE;
|
||||||
if (!o)
|
void* o = NULL;
|
||||||
LUMIERA_DIE (NO_MEMORY);
|
|
||||||
|
size_t gross = n*size;
|
||||||
|
|
||||||
|
if (n&&size)
|
||||||
|
do
|
||||||
|
o = calloc (n, size);
|
||||||
|
while (!o && lumiera_safeclib_resourcecollector_run_hook (LUMIERA_RESOURCE_MEMORY, &iteration, &gross));
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,9 +91,13 @@ lumiera_calloc (size_t n, size_t size)
|
||||||
void*
|
void*
|
||||||
lumiera_realloc (void* ptr, size_t size)
|
lumiera_realloc (void* ptr, size_t size)
|
||||||
{
|
{
|
||||||
void* o = size ? realloc (ptr, size) : NULL;
|
enum lumiera_resource_try iteration = LUMIERA_RESOURCE_ONE;
|
||||||
if (!o)
|
void* o = NULL;
|
||||||
LUMIERA_DIE (NO_MEMORY);
|
|
||||||
|
if (size)
|
||||||
|
do
|
||||||
|
o = realloc (ptr, size);
|
||||||
|
while (!o && lumiera_safeclib_resourcecollector_run_hook (LUMIERA_RESOURCE_MEMORY, &iteration, &size));
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
@ -65,14 +106,16 @@ lumiera_realloc (void* ptr, size_t size)
|
||||||
char*
|
char*
|
||||||
lumiera_strndup (const char* str, size_t len)
|
lumiera_strndup (const char* str, size_t len)
|
||||||
{
|
{
|
||||||
char* o;
|
enum lumiera_resource_try iteration = LUMIERA_RESOURCE_ONE;
|
||||||
if (str)
|
void* o = NULL;
|
||||||
|
|
||||||
|
do
|
||||||
|
if (str && len)
|
||||||
o = strndup (str, len);
|
o = strndup (str, len);
|
||||||
else
|
else
|
||||||
o = strdup ("");
|
o = strdup ("");
|
||||||
|
while (!o && lumiera_safeclib_resourcecollector_run_hook (LUMIERA_RESOURCE_MEMORY, &iteration, &len));
|
||||||
|
|
||||||
if (!o)
|
|
||||||
LUMIERA_DIE (NO_MEMORY);
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -234,3 +277,12 @@ lumiera_tmpbuf_tr (const char* in, const char* from, const char* to, const char*
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Local Variables:
|
||||||
|
// mode: C
|
||||||
|
// c-file-style: "gnu"
|
||||||
|
// indent-tabs-mode: nil
|
||||||
|
// End:
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,18 @@
|
||||||
*/
|
*/
|
||||||
LUMIERA_ERROR_DECLARE(NO_MEMORY);
|
LUMIERA_ERROR_DECLARE(NO_MEMORY);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Install the resourcecollector run hook.
|
||||||
|
* The resourcecollectr must be hooked into the safeclib at bootup after it got
|
||||||
|
* initialized and removed from it before shut down. Without resourcecollector
|
||||||
|
* failed allocations will abort().
|
||||||
|
* @param hook pointer to the resourcecollector_run function, must be of type
|
||||||
|
* lumiera_resourcecollector_run_fn but we dont want a dependency on backend in this header
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
lumiera_safeclib_set_resourcecollector (void* hook);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allocate memory.
|
* Allocate memory.
|
||||||
* always succeeds or dies
|
* always succeeds or dies
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue