lumiera_documentation/src/lib/safeclib.c
Ichthyostega 806db414dd Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
 * there is no entity "Lumiera.org" which holds any copyrights
 * Lumiera source code is provided under the GPL Version 2+

== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''

The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!

The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00

205 lines
5.8 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
safe_clib.c - Portable and safe wrapers around some clib functions and some tools
Copyright (C)
2008, Christian Thaeter <ct@pipapo.org>
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
* *****************************************************************/
/** @file safeclib.c
** Implementation of error-safe wrappers for some notorious C-Lib functions.
*/
#include "lib/error.h"
#include "lib/safeclib.h"
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <nobug.h>
LUMIERA_ERROR_DEFINE (NO_MEMORY, "Out of Memory!");
/**
* Resources known to the resource collector
* @todo 10/2023 this is a left-over of the »resource-collector« plan
*/
enum lumiera_resource
{
/** memory blocks, context is a pointer to the size_t required **/
LUMIERA_RESOURCE_MEMORY,
// /** OS filehandles **/
// LUMIERA_RESOURCE_FILEHANDLE,
// /** CPU time, as in threads and such **/
// LUMIERA_RESOURCE_CPU,
// /** mmaped regions **/
// LUMIERA_RESOURCE_MMAP,
// /** disk space for the storage area, context is a pointer to the filename indication the device **/
// LUMIERA_RESOURCE_DISKSTORAGE,
// /** disk bandwidth for the storage area, context is a pointer to the filename indication the device **/
// LUMIERA_RESOURCE_STORAGEBANDWIDTH,
// /** disk space for the caching area, context is a pointer to the filename indication the device **/
// LUMIERA_RESOURCE_DISKCACHE,
// /** disk bandwidth for the caching area, context is a pointer to the filename indication the device **/
// LUMIERA_RESOURCE_CACHEBANDWIDTH,
LUMIERA_RESOURCE_END /* last entry */
};
/**
* 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.
*/
enum lumiera_resource_try
{
/** No op, returned by a handler when it did nothing **/
LUMIERA_RESOURCE_NONE,
/** try to free one or really few of this resources **/
LUMIERA_RESOURCE_ONE,
/** try to free a small reasonable implementation defined amount of resources **/
LUMIERA_RESOURCE_SOME,
/** try to free a bigger implementation defined amount of resources **/
LUMIERA_RESOURCE_MANY,
/** free as much as possible **/
LUMIERA_RESOURCE_ALL,
/** die! **/
LUMIERA_RESOURCE_PANIC,
/** When a handler gets unregistered it will be called with this value to give it a chance to clean up the user 'data' **/
LUMIERA_RESOURCE_UNREGISTER
};
/**
* @internal 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.
*/
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 */
}
void*
lumiera_malloc (size_t size)
{
enum lumiera_resource_try iteration = LUMIERA_RESOURCE_ONE;
void* o = NULL;
if (size)
{
o = malloc (size);
if (!o)
die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &size);
}
return o;
}
void*
lumiera_calloc (size_t n, size_t size)
{
enum lumiera_resource_try iteration = LUMIERA_RESOURCE_ONE;
void* o = NULL;
size_t gross = n*size;
if (n&&size)
{
o = calloc (n, size);
if (!o)
die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &gross);
}
return o;
}
void*
lumiera_realloc (void* ptr, size_t size)
{
enum lumiera_resource_try iteration = LUMIERA_RESOURCE_ONE;
void* o = NULL;
if (size)
{
o = realloc (ptr, size);
if (!o)
die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &size);
}
return o;
}
char*
lumiera_strndup (const char* str, size_t len)
{
enum lumiera_resource_try iteration = LUMIERA_RESOURCE_ONE;
void* o = NULL;
if (str && len)
o = strndup (str, len);
else
o = strdup ("");
if (!o)
die_no_mem (LUMIERA_RESOURCE_MEMORY, &iteration, &len);
return o;
}
int
lumiera_strncmp (const char* a, const char* b, size_t len)
{
return a == b ? 0 : strncmp (a?a:"", b?b:"", len);
}
int
lumiera_streq (const char* a, const char* b)
{
return !lumiera_strncmp (a, b, SIZE_MAX);
}
/*
// Local Variables:
// mode: C
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
*/