ucx/mempool.c

changeset 152
62921b370c60
parent 124
80609f9675f1
child 157
0b33b9396851
equal deleted inserted replaced
151:11f3bb408051 152:62921b370c60
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2015 Olaf Wintermann. All rights reserved. 4 * Copyright 2016 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
91 91
92 return pool; 92 return pool;
93 } 93 }
94 94
95 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 95 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
96 if (newcap < pool->ndata) {
97 return 1;
98 }
99
96 void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); 100 void **data = (void**) realloc(pool->data, newcap*sizeof(void*));
97 if (data) { 101 if (data) {
98 pool->data = data; 102 pool->data = data;
99 pool->size = newcap; 103 pool->size = newcap;
100 return EXIT_SUCCESS; 104 return 0;
101 } else { 105 } else {
102 return EXIT_FAILURE; 106 return 1;
103 } 107 }
104 } 108 }
105 109
106 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { 110 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
107 if (pool->ndata >= pool->size) { 111 if (pool->ndata >= pool->size) {
108 // The hard coded 16 is documented for this function and ucx_mempool_new 112 size_t newcap = pool->size*2;
109 if (ucx_mempool_chcap(pool, pool->size + 16) == EXIT_FAILURE) { 113 if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) {
110 return NULL; 114 return NULL;
111 } 115 }
112 } 116 }
113 117
114 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); 118 void *p = malloc(sizeof(ucx_destructor) + n);
119 ucx_memchunk *mem = (ucx_memchunk*)p;
115 if (!mem) { 120 if (!mem) {
116 return NULL; 121 return NULL;
117 } 122 }
118 123
119 mem->destructor = NULL; 124 mem->destructor = NULL;
145 return newm + sizeof(ucx_destructor); 150 return newm + sizeof(ucx_destructor);
146 } 151 }
147 } 152 }
148 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", 153 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
149 (intptr_t)ptr, (intptr_t)pool); 154 (intptr_t)ptr, (intptr_t)pool);
150 exit(EXIT_FAILURE); 155 abort();
151 } else { 156 } else {
152 return newm + sizeof(ucx_destructor); 157 return newm + sizeof(ucx_destructor);
153 } 158 }
154 } 159 }
155 160
170 return; 175 return;
171 } 176 }
172 } 177 }
173 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", 178 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
174 (intptr_t)ptr, (intptr_t)pool); 179 (intptr_t)ptr, (intptr_t)pool);
175 exit(EXIT_FAILURE); 180 abort();
176 } 181 }
177 182
178 void ucx_mempool_destroy(UcxMempool *pool) { 183 void ucx_mempool_destroy(UcxMempool *pool) {
179 ucx_memchunk *chunk; 184 ucx_memchunk *chunk;
180 for(size_t i=0 ; i<pool->ndata ; i++) { 185 for(size_t i=0 ; i<pool->ndata ; i++) {

mercurial