OpenDNSSEC-enforcer
1.3.8
|
00001 /* 00002 * $Id: memory.c 763 2009-05-22 13:03:15Z sion $ 00003 * 00004 * Copyright (c) 2008-2009 Nominet UK. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00029 /*+ 00030 * Filename: memory.c 00031 * 00032 * Description: 00033 * Wrappers around memory allocation routines. If any fail, 00034 * the program will write a memory exhaustion error to stderr 00035 * and terminate. 00036 -*/ 00037 00038 #include <stdio.h> 00039 #include <stdlib.h> 00040 00041 #include "ksm/memory.h" 00042 #include "ksm/ksmdef.h" 00043 #include "ksm/message.h" 00044 00045 /*+ 00046 * MemMalloc - Allocate Memory 00047 * MemCalloc - Allocate Contiguous Memory 00048 * MemRealloc - Reallocate Memory 00049 * 00050 * Description: 00051 * Wrapper routines around the Unix memory allocation routines. If 00052 * an allocation routine returns null, an error is logged and the 00053 * program exits. 00054 * 00055 * MemFree is also defined, but as a macro to allow the passed element 00056 * to be zeroed. The definition can be found in the header file. 00057 -*/ 00058 00059 void* MemMalloc(size_t size) 00060 { 00061 void *ptr = malloc(size); 00062 if (ptr == NULL) { 00063 MsgLog(KSM_STMTALLOC, "malloc: Out of swap space"); 00064 fprintf(stderr, "malloc: Out of swap space"); 00065 exit(1); 00066 } 00067 return ptr; 00068 } 00069 00070 void* MemCalloc(size_t nmemb, size_t size) 00071 { 00072 void *ptr = calloc(nmemb, size); 00073 if (ptr == NULL) { 00074 MsgLog(KSM_STMTALLOC, "calloc: Out of swap space"); 00075 fprintf(stderr, "calloc: Out of swap space"); 00076 exit(1); 00077 } 00078 return ptr; 00079 } 00080 00081 void* MemRealloc(void *ptr, size_t size) 00082 { 00083 void *ptr1 = realloc(ptr, size); 00084 if (ptr1 == NULL) { 00085 MsgLog(KSM_STMTALLOC, "realloc: Out of swap space"); 00086 fprintf(stderr, "realloc: Out of swap space"); 00087 exit(1); 00088 } 00089 return ptr1; 00090 }