123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /* $Id: q_malloc.h 5897 2009-07-20 14:52:34Z bogdan_iancu $
- *
- * simple & fast malloc library
- *
- * Copyright (C) 2001-2003 FhG Fokus
- *
- * This file is part of opensips, a free SIP server.
- *
- * opensips is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * opensips is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * History:
- * --------
- * 2003-05-21 on sparc64 roundto 8 even in debugging mode (so malloc'ed
- * long longs will be 64 bit aligned) (andrei)
- * 2004-07-19 support for 64 bit (2^64 mem. block) and more info
- * for the future de-fragmentation support (andrei)
- * 2004-11-10 support for > 4Gb mem. (switched to long) (andrei)
- */
- #if !defined(q_malloc_h)
- #define q_malloc_h
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "config.h"
- /* defs*/
- #ifdef NO_FIXED_ADDR
- //#define RELATIVE_ADDR //there are no help between different processes
- #endif // NO_FIXED_ADDR
- /* size we round to, must be = 2^n
- and also sizeof(qm_frag)+sizeof(qm_frag_end) must be multiple of ROUNDTO!*/
- #define ROUNDTO 16UL
- #define MIN_FRAG_SIZE ROUNDTO
- #define QM_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
- #define QM_MALLOC_OPTIMIZE ((unsigned long)(1UL<<QM_MALLOC_OPTIMIZE_FACTOR))
- /* size to optimize for,
- (most allocs <= this size),
- must be 2^k */
- #define QM_HASH_SIZE ((unsigned long)(QM_MALLOC_OPTIMIZE/ROUNDTO + \
- (sizeof(long)*8-QM_MALLOC_OPTIMIZE_FACTOR)+1))
- /* hash structure:
- * 0 .... QM_MALLOC_OPTIMIE/ROUNDTO - small buckets, size increases with
- * ROUNDTO from bucket to bucket
- * +1 .... end - size = 2^k, big buckets */
- struct qm_frag{
- unsigned long size;
- union{
- struct qm_frag* nxt_free;
- long is_free;
- }u;
- };
- struct qm_frag_end{
- unsigned long size;
- struct qm_frag* prev_free;
- };
- struct qm_frag_lnk{
- struct qm_frag head;
- struct qm_frag_end tail;
- unsigned long no;
- };
- struct qm_block{
- unsigned long size; /* total size */
- unsigned long used; /* alloc'ed size*/
- unsigned long real_used; /* used+malloc overhead*/
- unsigned long max_real_used;
-
- struct qm_frag* first_frag;
- struct qm_frag_end* last_frag_end;
-
- struct qm_frag_lnk free_hash[QM_HASH_SIZE];
- /*struct qm_frag_end free_lst_end;*/
- long lock;
- void *user_data[8];
- };
- /* init malloc and return a qm_block*/
- struct qm_block* qm_malloc_init(char* address, unsigned long size, int newcreate);
- void* qm_malloc(struct qm_block*, unsigned long size);
- void qm_free(struct qm_block*, void* p);
- void qm_lock(struct qm_block*);
- void qm_unlock(struct qm_block*);
- void qm_set_user_data(struct qm_block* qm, int idx, void* user_data);
- void* qm_get_user_data(struct qm_block* qm, int idx);
- #ifdef __cplusplus
- } // extern "C" {
- #endif
- #endif
|