gclib/mem.c
author ali <ali@juiblex.co.uk>
Wed Jan 25 19:33:43 2012 +0000 (2012-01-25)
changeset 2 cbd63f7ba40f
permissions -rw-r--r--
Don't test that gutcheck ignores paragraphs with only short lines
ali@0
     1
#include <stdlib.h>
ali@0
     2
#include <stdio.h>
ali@0
     3
#include <string.h>
ali@0
     4
#include <gclib/mem.h>
ali@0
     5
ali@0
     6
/*
ali@0
     7
 * A memory allocator that aborts on failure (so that the caller never
ali@0
     8
 * needs to handle out of memory, which we assume is very unlikely to
ali@0
     9
 * happen under normal circumstances on any modern machine).
ali@0
    10
 */
ali@0
    11
void *mem_alloc(size_t nmemb,size_t size)
ali@0
    12
{
ali@0
    13
    void *ptr=malloc(nmemb*size);
ali@0
    14
    if (!ptr)
ali@0
    15
    {
ali@0
    16
	fprintf(stderr,
ali@0
    17
	  "Not enough memory to allocate %lu elements of %lu bytes.\n",
ali@0
    18
	  (unsigned long)nmemb,(unsigned long)size);
ali@0
    19
	abort();
ali@0
    20
    }
ali@0
    21
    return ptr;
ali@0
    22
}
ali@0
    23
ali@0
    24
/*
ali@0
    25
 * As mem_new, but new memory is cleared to zero.
ali@0
    26
 */
ali@0
    27
void *mem_alloc0(size_t nmemb,size_t size)
ali@0
    28
{
ali@0
    29
    void *ptr=calloc(nmemb,size);
ali@0
    30
    if (!ptr)
ali@0
    31
    {
ali@0
    32
	fprintf(stderr,
ali@0
    33
	  "Not enough memory to allocate %lu elements of %lu bytes.\n",
ali@0
    34
	  (unsigned long)nmemb,(unsigned long)size);
ali@0
    35
	abort();
ali@0
    36
    }
ali@0
    37
    return ptr;
ali@0
    38
}
ali@0
    39
ali@0
    40
/*
ali@0
    41
 * Grow or shrink a memory block, aborting on failure.
ali@0
    42
 */
ali@0
    43
void *mem_realloc(void *ptr,size_t nmemb,size_t size)
ali@0
    44
{
ali@0
    45
    ptr=realloc(ptr,nmemb*size);
ali@0
    46
    if (!ptr)
ali@0
    47
    {
ali@0
    48
	fprintf(stderr,
ali@0
    49
	  "Not enough memory to allocate %lu elements of %lu bytes.\n",
ali@0
    50
	  (unsigned long)nmemb,(unsigned long)size);
ali@0
    51
	abort();
ali@0
    52
    }
ali@0
    53
    return ptr;
ali@0
    54
}