OpenDNSSEC-signer  1.3.16
allocator.c
Go to the documentation of this file.
1 /*
2  * $Id: allocator.c 3817 2010-08-27 08:43:00Z matthijs $
3  *
4  * Copyright (c) 2010-2011 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
34 #include "config.h"
35 #include "shared/allocator.h"
36 #include "shared/log.h"
37 
38 #include <stdlib.h>
39 #include <string.h>
40 
41 static const char* allocator_str = "allocator";
42 
48 allocator_create(void *(*allocator)(size_t size), void (*deallocator)(void *))
49 {
50  allocator_type* result =
51  (allocator_type*) allocator(sizeof(allocator_type));
52  if (!result) {
53  ods_log_error("[%s] failed to create allocator", allocator_str);
54  return NULL;
55  }
56  result->allocator = allocator;
57  result->deallocator = deallocator;
58  return result;
59 }
60 
61 
66 void*
67 allocator_alloc(allocator_type* allocator, size_t size)
68 {
69  void* result;
70 
71  ods_log_assert(allocator);
72  /* align size */
73  if (size == 0) {
74  size = 1;
75  }
76  result = allocator->allocator(size);
77  if (!result) {
78  ods_fatal_exit("[%s] allocator failed: out of memory", allocator_str);
79  return NULL;
80  }
81  return result;
82 }
83 
84 
89 void*
90 allocator_alloc_zero(allocator_type *allocator, size_t size)
91 {
92  void *result = allocator_alloc(allocator, size);
93  if (!result) {
94  return NULL;
95  }
96  memset(result, 0, size);
97  return result;
98 }
99 
100 
105 void*
106 allocator_alloc_init(allocator_type *allocator, size_t size, const void *init)
107 {
108  void *result = allocator_alloc(allocator, size);
109  if (!result) {
110  return NULL;
111  }
112  memcpy(result, init, size);
113  return result;
114 }
115 
116 
121 char*
122 allocator_strdup(allocator_type *allocator, const char *string)
123 {
124  if (!string) {
125  return NULL;
126  }
127  return (char*) allocator_alloc_init(allocator, strlen(string) + 1, string);
128 }
129 
130 
135 void
136 allocator_deallocate(allocator_type *allocator, void* data)
137 {
138  ods_log_assert(allocator);
139 
140  if (!data) {
141  return;
142  }
143  allocator->deallocator(data);
144  return;
145 }
146 
147 
152 void
154 {
155  void (*deallocator)(void *);
156  if (!allocator) {
157  return;
158  }
159  deallocator = allocator->deallocator;
160  deallocator(allocator);
161  return;
162 }
163 
void * allocator_alloc_zero(allocator_type *allocator, size_t size)
Definition: allocator.c:90
void *(* allocator)(size_t)
Definition: allocator.h:43
void(* deallocator)(void *)
Definition: allocator.h:44
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:67
void ods_fatal_exit(const char *format,...)
Definition: log.c:397
void ods_log_error(const char *format,...)
Definition: log.c:349
allocator_type * allocator_create(void *(*allocator)(size_t size), void(*deallocator)(void *))
Definition: allocator.c:48
char * allocator_strdup(allocator_type *allocator, const char *string)
Definition: allocator.c:122
void allocator_cleanup(allocator_type *allocator)
Definition: allocator.c:153
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:136
void * allocator_alloc_init(allocator_type *allocator, size_t size, const void *init)
Definition: allocator.c:106
#define ods_log_assert(x)
Definition: log.h:141