|
| 1 | +/* |
| 2 | + * Copyright (C) 2001-2003 FhG Fokus |
| 3 | + * Copyright (C) 2025 OpenSIPS Project |
| 4 | + * |
| 5 | + * This file is part of opensips, a free SIP server. |
| 6 | + * |
| 7 | + * opensips is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version |
| 11 | + * |
| 12 | + * opensips is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + */ |
| 21 | + |
| 22 | +#ifdef F_PARALLEL_MALLOC |
| 23 | + |
| 24 | +#include <string.h> |
| 25 | +#include <stdio.h> |
| 26 | +#include <stdlib.h> |
| 27 | + |
| 28 | +#include "f_parallel_malloc.h" |
| 29 | +#include "../dprint.h" |
| 30 | +#include "../globals.h" |
| 31 | +#include "../statistics.h" |
| 32 | + |
| 33 | +#ifdef DBG_MALLOC |
| 34 | +#include "mem_dbg_hash.h" |
| 35 | +#endif |
| 36 | + |
| 37 | +#include "../lib/dbg/struct_hist.h" |
| 38 | + |
| 39 | +#define MIN_FRAG_SIZE ROUNDTO |
| 40 | +#define F_PARALLEL_FRAG_OVERHEAD (sizeof(struct parallel_frag)) |
| 41 | +#define frag_is_free(_f) ((_f)->prev) |
| 42 | + |
| 43 | +#define F_PARALLEL_FRAG_NEXT(f) \ |
| 44 | + ((struct parallel_frag *)((char *)(f) + sizeof(struct parallel_frag) + (f)->size)) |
| 45 | + |
| 46 | +#define max(a,b) ( (a)>(b)?(a):(b)) |
| 47 | + |
| 48 | +/* ROUNDTO= 2^k so the following works */ |
| 49 | +#define ROUNDTO_MASK (~((unsigned long)ROUNDTO-1)) |
| 50 | +#define ROUNDUP(s) (((s)+(ROUNDTO-1))&ROUNDTO_MASK) |
| 51 | +#define ROUNDDOWN(s) ((s)&ROUNDTO_MASK) |
| 52 | + |
| 53 | +/* finds the hash value for s, s=ROUNDTO multiple*/ |
| 54 | +#define F_PARALLEL_GET_HASH(s) ( ((unsigned long)(s)<=F_PARALLEL_MALLOC_OPTIMIZE)?\ |
| 55 | + (unsigned long)(s)/ROUNDTO: \ |
| 56 | + F_PARALLEL_MALLOC_OPTIMIZE/ROUNDTO+big_hash_idx((s))- \ |
| 57 | + F_PARALLEL_MALLOC_OPTIMIZE_FACTOR+1 ) |
| 58 | + |
| 59 | +#define F_PARALLEL_UN_HASH(h) ( ((unsigned long)(h)<=(F_PARALLEL_MALLOC_OPTIMIZE/ROUNDTO))?\ |
| 60 | + (unsigned long)(h)*ROUNDTO: \ |
| 61 | + 1UL<<((unsigned long)(h)-F_PARALLEL_MALLOC_OPTIMIZE/ROUNDTO+\ |
| 62 | + F_PARALLEL_MALLOC_OPTIMIZE_FACTOR-1)\ |
| 63 | + ) |
| 64 | + |
| 65 | +static inline void parallel_free_minus(struct parallel_block *fm, unsigned long size) |
| 66 | +{ |
| 67 | + |
| 68 | + #if defined(DBG_MALLOC) || defined(STATISTICS) |
| 69 | + fm->real_used+=size; |
| 70 | + fm->used+=size; |
| 71 | + #endif |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +static inline void parallel_free_plus(struct parallel_block *fm, unsigned long size) |
| 76 | +{ |
| 77 | + |
| 78 | + #if defined(DBG_MALLOC) || defined(STATISTICS) |
| 79 | + fm->real_used-=size; |
| 80 | + fm->used-=size; |
| 81 | + #endif |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +/* computes hash number for big buckets*/ |
| 86 | +inline static unsigned long big_hash_idx(unsigned long s) |
| 87 | +{ |
| 88 | + unsigned long idx; |
| 89 | + /* s is rounded => s = k*2^n (ROUNDTO=2^n) |
| 90 | + * index= i such that 2^i > s >= 2^(i-1) |
| 91 | + * |
| 92 | + * => index = number of the first non null bit in s*/ |
| 93 | + idx=sizeof(long)*8-1; |
| 94 | + for (; !(s&(1UL<<(sizeof(long)*8-1))) ; s<<=1, idx--); |
| 95 | + return idx; |
| 96 | +} |
| 97 | + |
| 98 | +#ifdef SHM_EXTRA_STATS |
| 99 | +#include "module_info.h" |
| 100 | +unsigned long parallel_stats_get_index(void *ptr) |
| 101 | +{ |
| 102 | + if (!ptr) |
| 103 | + return GROUP_IDX_INVALID; |
| 104 | + |
| 105 | + return F_PARALLEL_FRAG(ptr)->statistic_index; |
| 106 | +} |
| 107 | + |
| 108 | +void parallel_stats_set_index(void *ptr, unsigned long idx) |
| 109 | +{ |
| 110 | + if (!ptr) |
| 111 | + return; |
| 112 | + |
| 113 | + F_PARALLEL_FRAG(ptr)->statistic_index = idx; |
| 114 | +} |
| 115 | +#endif |
| 116 | + |
| 117 | +static inline void parallel_insert_free(struct parallel_block *fm, struct parallel_frag *frag) |
| 118 | +{ |
| 119 | + struct parallel_frag **f; |
| 120 | + int hash; |
| 121 | + |
| 122 | + hash=F_PARALLEL_GET_HASH(frag->size); |
| 123 | + f=&(fm->free_hash[hash].first); |
| 124 | + if (*f) |
| 125 | + (*f)->block_ptr = fm; |
| 126 | + if (frag->size > F_PARALLEL_MALLOC_OPTIMIZE){ /* because of '<=' in GET_HASH, |
| 127 | + (different from 0.8.1[24] on |
| 128 | + purpose --andrei ) */ |
| 129 | + for(; *f; f=&((*f)->u.nxt_free)){ |
| 130 | + (*f)->block_ptr = fm; |
| 131 | + if (frag->size <= (*f)->size) break; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /*insert it here*/ |
| 136 | + if (*f) { |
| 137 | + (*f)->block_ptr = fm; |
| 138 | + } |
| 139 | + |
| 140 | + frag->prev = f; |
| 141 | + frag->u.nxt_free=*f; |
| 142 | + frag->block_ptr = fm; |
| 143 | + |
| 144 | + if( *f ) { |
| 145 | + (*f)->block_ptr = fm; |
| 146 | + (*f)->prev = &(frag->u.nxt_free); |
| 147 | + frag->u.nxt_free->block_ptr = fm; |
| 148 | + } |
| 149 | + |
| 150 | + *f=frag; |
| 151 | + fm->free_hash[hash].no++; |
| 152 | + |
| 153 | + frag->block_ptr = fm; |
| 154 | + parallel_free_plus(fm, frag->size); |
| 155 | +} |
| 156 | + |
| 157 | +static inline void parallel_remove_free(struct parallel_block *fm, struct parallel_frag *n) |
| 158 | +{ |
| 159 | + struct parallel_frag **pf; |
| 160 | + int hash; |
| 161 | + |
| 162 | + pf = n->prev; |
| 163 | + hash = F_PARALLEL_GET_HASH( n->size ); |
| 164 | + |
| 165 | + /* detach */ |
| 166 | + if (*pf) |
| 167 | + (*pf)->block_ptr=fm; |
| 168 | + |
| 169 | + *pf=n->u.nxt_free; |
| 170 | + if (*pf) |
| 171 | + (*pf)->block_ptr=fm; |
| 172 | + |
| 173 | + if( n->u.nxt_free ) |
| 174 | + n->u.nxt_free->prev = pf; |
| 175 | + |
| 176 | + fm->free_hash[hash].no--; |
| 177 | + |
| 178 | + n->prev = NULL; |
| 179 | + n->block_ptr = fm; |
| 180 | + |
| 181 | + parallel_free_minus(fm , n->size); |
| 182 | + |
| 183 | +}; |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +/* init malloc and return a parallel_block*/ |
| 190 | +struct parallel_block *parallel_malloc_init(char *address, unsigned long size, char *name, int idx) |
| 191 | + |
| 192 | +{ |
| 193 | + char *start; |
| 194 | + char *end; |
| 195 | + struct parallel_block *fm; |
| 196 | + unsigned long init_overhead; |
| 197 | + |
| 198 | + /* make address and size multiple of 8*/ |
| 199 | + start=(char*)ROUNDUP((unsigned long) address); |
| 200 | + LM_DBG("F_OPTIMIZE=%lu, /ROUNDTO=%lu, %lu-bytes aligned\n", |
| 201 | + F_PARALLEL_MALLOC_OPTIMIZE, F_PARALLEL_MALLOC_OPTIMIZE/ROUNDTO, |
| 202 | + (unsigned long)ROUNDTO); |
| 203 | + LM_DBG("F_HASH_SIZE=%lu, parallel_block size=%zu, frag_size=%zu\n", |
| 204 | + F_PARALLEL_HASH_SIZE, sizeof(struct parallel_block), sizeof(struct parallel_frag)); |
| 205 | + LM_DBG("params (%p, %lu), start=%p\n", address, size, start); |
| 206 | + |
| 207 | + if (size<(unsigned long)(start-address)) return 0; |
| 208 | + size-=(start-address); |
| 209 | + if (size <(MIN_FRAG_SIZE+F_PARALLEL_FRAG_OVERHEAD)) return 0; |
| 210 | + size=ROUNDDOWN(size); |
| 211 | + |
| 212 | + init_overhead=(ROUNDUP(sizeof(struct parallel_block))+ 2 * F_PARALLEL_FRAG_OVERHEAD); |
| 213 | + |
| 214 | + |
| 215 | + if (size < init_overhead) |
| 216 | + { |
| 217 | + /* not enough mem to create our control structures !!!*/ |
| 218 | + return 0; |
| 219 | + } |
| 220 | + end=start+size; |
| 221 | + fm=(struct parallel_block *)start; |
| 222 | + memset(fm, 0, sizeof(struct parallel_block)); |
| 223 | + fm->name = name; |
| 224 | + fm->size=size; |
| 225 | + |
| 226 | + fm->idx = idx; |
| 227 | + |
| 228 | + #if defined(DBG_MALLOC) || defined(STATISTICS) |
| 229 | + fm->used=size-init_overhead; |
| 230 | + fm->real_used=size; |
| 231 | + fm->max_real_used=init_overhead; |
| 232 | + fm->fragments = 0; |
| 233 | + #endif |
| 234 | + |
| 235 | + fm->first_frag=(struct parallel_frag *)(start+ROUNDUP(sizeof(struct parallel_block))); |
| 236 | + fm->last_frag=(struct parallel_frag *)(end-sizeof(struct parallel_frag)); |
| 237 | + |
| 238 | + fm->first_frag->block_ptr = fm; |
| 239 | + fm->last_frag->block_ptr = fm; |
| 240 | + |
| 241 | + /* init initial fragment*/ |
| 242 | + fm->first_frag->size=size-init_overhead; |
| 243 | + fm->last_frag->size=0; |
| 244 | + |
| 245 | + fm->last_frag->prev=NULL; |
| 246 | + fm->first_frag->prev=NULL; |
| 247 | + |
| 248 | + /* link initial fragment into the free list*/ |
| 249 | + |
| 250 | + parallel_insert_free(fm, fm->first_frag); |
| 251 | + |
| 252 | + return fm; |
| 253 | +} |
| 254 | + |
| 255 | +#include "f_parallel_malloc_dyn.h" |
| 256 | + |
| 257 | +#if !defined INLINE_ALLOC && defined DBG_MALLOC |
| 258 | +#undef DBG_MALLOC |
| 259 | +#include "f_parallel_malloc_dyn.h" |
| 260 | +#define DBG_MALLOC |
| 261 | +#endif |
| 262 | + |
| 263 | +#ifdef SHM_EXTRA_STATS |
| 264 | +void parallel_stats_core_init(struct parallel_block *fm, int core_index) |
| 265 | +{ |
| 266 | + struct parallel_frag *f; |
| 267 | + |
| 268 | + for (f=fm->first_frag; (char *)f < (char *)fm->last_frag; f=F_PARALLEL_FRAG_NEXT(f)) |
| 269 | + if (!frag_is_free(f)) |
| 270 | + f->statistic_index = core_index; |
| 271 | +} |
| 272 | + |
| 273 | +#endif |
| 274 | + |
| 275 | + |
| 276 | + |
| 277 | + |
| 278 | +/* fills a malloc info structure with info about the block |
| 279 | + * if a parameter is not supported, it will be filled with 0 */ |
| 280 | +void parallel_info(struct parallel_block *fm, struct mem_info *info) |
| 281 | +{ |
| 282 | + memset(info,0, sizeof(*info)); |
| 283 | + /* TODO - Not implemented, need an array here */ |
| 284 | + return; |
| 285 | +} |
| 286 | + |
| 287 | +#endif |
0 commit comments