sf_mem(3)

NAME
sf_malloc, sf_calloc, sf_realloc, sf_strdup, strndup, strfunc_ctl - string duplication and safe memory allocation.

SYNOPSIS
#include <strfunc.h>

Safe malloc(3) analog:
void *
sf_malloc(size_t size);

Safe calloc(3) analog:
void *
sf_calloc(size_t number, size_t size);

Safe realloc(3) analog:
void *
sf_realloc(void *ptr, size_t size);

Duplicate a specified number of characters from the string:
char *
strndup(const char *a, size_t num);

Safe strdup(3) analog:
char *
sf_strdup(const char *a);

int
strfunc_ctl(int request, int *optArg);

DESCRIPTION
Those functions are used internally by virtually all libstrfunc functions to manipulate memory. They are wrappers around the native library calls malloc(3), calloc(3), realloc(3) and provide additional flexibility in those cases when system is low in memory.

strndup() used to copy the specified number of characters to a newly-created buffer.

sf_strdup(), sf_malloc() and sf_realloc() are used instead of strdup(3), malloc(3) and realloc(3) analogs to achieve more control and safety when computer becomes low in memory.

All functions are defaulted to call abort(3) upon the unsatisfied memory request. This default behavior can be easily changed by using strfunc_ctl() call with SF_SET_MEMORY_FAILURE_BEHAVIOR.

int strfunc_ctl(int request, int *optArg) used to change default behaviour of the previously described functions in cases of resource shortage.

The request argument can be the following constant:

    	SF_GET_MEMORY_FAILURE_BEHAVIOR
    	SF_SET_MEMORY_FAILURE_BEHAVIOR
    	SF_GET_MEMORY_FAILURE_TRIES
    	SF_SET_MEMORY_FAILURE_TRIES

to get or set the memory allocation behaviour appropriately. While the third and fourth values are used to get or specify the number of tries of allocating resources before falling into a failure case, the first two values can be used to switch the default behaviour to call abort(3) in case of failure. SF_GET_MEMORY_FAILURE_BEHAVIOR returns with and SF_SET_MEMORY_FAILURE_BEHAVIOR accept the following values:

    	SF_ARG_MFB_ABORT      /* call abort(3) on failure, the default */
    	SF_ARG_MFB_ENOMEM     /* return NULL with errno set to ENOMEM */
    	SF_ARG_MFB_TRY_ABORT  /* try N times before calling abort(3) */
    	SF_ARG_MFB_TRY_ENOMEM /* do the same before returning an error */
    	SF_ARG_MFB_TRY_NOFAIL /* loop indefinitely */

Again, virually all functions defined in libstrfunc are aware of this memory control technique, so, for example, you may respect that some strfunc library function will not return NULL when you've earlier executed something like

      int memory_control_type = SF_ARG_MFB_TRY_NOFAIL;
      strfunc_ctl(SF_SET_MEMORY_FAILURE_BEHAVIOR, &memory_control_type);

SEE ALSO
strfunc(3), malloc(3).

AUTHORS
Lev Walkin <vlm@spelio.net.ru>