"Fossies" - the Fresh Open Source Software Archive

Member "quotactl-1.00/giraffe/include/mallocvar.h" (12 Apr 2005, 2651 Bytes) of package /linux/privat/old/quotactl-1.00.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mallocvar.h" see the Fossies "Dox" file reference documentation.

    1 /* These are some dynamic memory allocation facilities.  They are essentially
    2    an extension to C, as they do allocations with a cognizance of C 
    3    variables.  You can use them to make C read more like a high level
    4    language.
    5 */
    6 
    7 #ifndef MALLOCVAR_INCLUDED
    8 #define MALLOCVAR_INCLUDED
    9 
   10 #include <limits.h>
   11 #include <stdlib.h>
   12 
   13 static __inline__ void
   14 mallocProduct(void **      const resultP, 
   15               unsigned int const factor1,
   16               unsigned int const factor2) {
   17 /*----------------------------------------------------------------------------
   18    malloc a space whose size in bytes is the product of 'factor1' and
   19    'factor2'.  But if that size cannot be represented as an unsigned int,
   20    return NULL without allocating anything.  Also return NULL if the malloc
   21    fails.
   22 
   23    If either factor is zero, malloc a single byte.
   24 
   25    Note that malloc() actually takes a size_t size argument, so the
   26    proper test would be whether the size can be represented by size_t,
   27    not unsigned int.  But there is no reliable indication available to
   28    us, like UINT_MAX, of what the limitations of size_t are.  We
   29    assume size_t is at least as expressive as unsigned int and that
   30    nobody really needs to allocate more than 4GB of memory.
   31 -----------------------------------------------------------------------------*/
   32     if (factor1 == 0 || factor2 == 0)
   33         *resultP = malloc(1);
   34     else {
   35         if (UINT_MAX / factor2 < factor1) 
   36             *resultP = NULL;
   37         else 
   38             *resultP = malloc(factor1 * factor2); 
   39     }
   40 }
   41 
   42 
   43 
   44 static __inline__ void
   45 reallocProduct(void **      const blockP,
   46                unsigned int const factor1,
   47                unsigned int const factor2) {
   48     
   49     if (UINT_MAX / factor2 < factor1) 
   50         *blockP = NULL;
   51     else 
   52         *blockP = realloc(*blockP, factor1 * factor2); 
   53 }
   54 
   55 
   56 
   57 #define MALLOCARRAY(arrayName, nElements) do { \
   58     void * array; \
   59     mallocProduct(&array, nElements, sizeof(arrayName[0])); \
   60     arrayName = array; \
   61 } while (0)
   62 
   63 #define REALLOCARRAY(arrayName, nElements) \
   64     reallocProduct((void **)&arrayName, nElements, sizeof(arrayName[0]))
   65 
   66 
   67 #define MALLOCARRAY_NOFAIL(arrayName, nElements) \
   68 do { \
   69     MALLOCARRAY(arrayName, nElements); \
   70     if ((arrayName) == NULL) \
   71         abort(); \
   72 } while(0)
   73 
   74 #define REALLOCARRAY_NOFAIL(arrayName, nElements) \
   75 do { \
   76     REALLOCARRAY(arrayName, nElements); \
   77     if ((arrayName) == NULL) \
   78         abort(); \
   79 } while(0)
   80 
   81 
   82 #define MALLOCVAR(varName) \
   83     varName = malloc(sizeof(*varName))
   84 
   85 #define MALLOCVAR_NOFAIL(varName) \
   86     do {if ((varName = malloc(sizeof(*varName))) == NULL) abort();} while(0)
   87 
   88 #endif
   89