alloc.c
来自「CS架构的多平台的GUI系统」· C语言 代码 · 共 247 行
C
247 行
/*************************************************************************** begin : Tue Oct 4 2005 copyright : (C) 2005 by Alper Akcan email : distchx@yahoo.com ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation; either version 2.1 of the * * License, or (at your option) any later version. * * * ***************************************************************************/#include <stdlib.h>#include "xynth_.h"#if defined(DEBUG_ALLOC)typedef struct s_alloc_s { void *addr; unsigned int size;} s_alloc_t;s_list_t *alcl = NULL;unsigned int cnt_free = 0;unsigned int cnt_alloc = 0;s_thread_mutex_t *mut_alloc = NULL;static int s_alloc_list_remove (s_list_t *li, int pos){ int i = 0; s_list_node_t *ntmp; if ((li == NULL) || (pos < 0) || (pos >= li->nb_elt)) { /* element does not exist */ return -1; } /* exist because nb_elt > 0 */ ntmp = li->node; if ((pos == 0)) { /* special case */ li->node = (s_list_node_t *) ntmp->next; li->nb_elt--; free(ntmp); return li->nb_elt; } while (pos > (i + 1)) { i++; ntmp = (s_list_node_t *) ntmp->next; } /* insert new node */ { s_list_node_t *remnode; remnode = (s_list_node_t *) ntmp->next; ntmp->next = ((s_list_node_t *) ntmp->next)->next; free(remnode); li->nb_elt--; } return li->nb_elt;}static int s_alloc_list_add (s_list_t *li, void *el, int pos){ int i = 0; s_list_node_t *ntmp; if (li == NULL) { return -1; } if ((pos == -1) || (pos >= li->nb_elt)) { /* insert at the end */ pos = li->nb_elt; } if (li->nb_elt == 0) { li->node = (s_list_node_t *) malloc(sizeof(s_list_node_t)); li->node->element = el; li->nb_elt++; return li->nb_elt; } /* exist because nb_elt > 0 */ ntmp = li->node; if (pos == 0) { li->node = (s_list_node_t *) malloc(sizeof(s_list_node_t)); li->node->element = el; li->node->next = ntmp; li->nb_elt++; return li->nb_elt; } /* pos = 0 insert before first elt */ while (pos > (i + 1)) { i++; /* when pos > i next node exist */ ntmp = (s_list_node_t *) ntmp->next; } /* if pos == nb_elt next node does not exist */ if (pos == li->nb_elt) { ntmp->next = (s_list_node_t *) malloc(sizeof(s_list_node_t)); ntmp = (s_list_node_t *) ntmp->next; ntmp->element = el; li->nb_elt++; return li->nb_elt; } /* here pos == i so next node is where we want to insert new node */ { s_list_node_t *nextnode = (s_list_node_t *) ntmp->next; ntmp->next = (s_list_node_t *) malloc(sizeof(s_list_node_t)); ntmp = (s_list_node_t *) ntmp->next; ntmp->element = el; ntmp->next = nextnode; li->nb_elt++; } return li->nb_elt;}#define LOCK_ALLOC() {\ if (alcl == NULL) {\ alcl = (s_list_t *) malloc(sizeof(s_list_t));\ s_list_init(alcl);\ }\ if (mut_alloc == NULL) {\ mut_alloc = (s_thread_mutex_t *) malloc(sizeof(s_thread_t));\ s_thread_sys_mutex_init(mut_alloc);\ }\ s_thread_mutex_lock(mut_alloc);\}#define UNLOCK_ALLOC() {\ s_thread_mutex_unlock(mut_alloc);\}void s_alloc_debug (void){ int pos; s_alloc_t *alc; unsigned int mem; LOCK_ALLOC(); pos = 0; mem = 0; while (!s_list_eol(alcl, pos)) { alc = (s_alloc_t *) s_list_get(alcl, pos); mem += alc->size; pos++; } debugf(DALC, "alloc: %u, free: %u, allocated: %u", cnt_alloc, cnt_free, mem); UNLOCK_ALLOC();}#endif /* DEBUG_ALLOC */void * s_malloc (unsigned int size){ void *ret; ret = malloc(size); if (ret == NULL) { debugf(DFAT, "Not enough memory!"); }#if defined(DEBUG_ALLOC) { s_alloc_t *alc = (s_alloc_t *) malloc(sizeof(s_alloc_t)); alc->addr = ret; alc->size = size; LOCK_ALLOC(); s_alloc_list_add(alcl, alc, -1); cnt_alloc++; UNLOCK_ALLOC(); }#endif return ret;}void * s_calloc (unsigned int nmemb, unsigned int size){ void *ret; ret = s_malloc(nmemb * size); if (ret == NULL) { debugf(DFAT, "Not enough memory!"); } memset(ret, 0, nmemb * size); return ret;}void * s_realloc (void *ptr, unsigned int size){ void *ret; ret = realloc(ptr, size); if (ret == NULL) { debugf(DFAT, "Not enough memory!"); }#if defined(DEBUG_ALLOC) { int pos = 0; s_alloc_t *alc = (s_alloc_t *) malloc(sizeof(s_alloc_t)); alc->addr = ret; alc->size = size; LOCK_ALLOC(); while (!s_list_eol(alcl, pos)) { s_alloc_t *tmp = (s_alloc_t *) s_list_get(alcl, pos); if (tmp->addr == ptr) { s_alloc_list_remove(alcl, pos); break; } pos++; } s_alloc_list_add(alcl, alc, -1); UNLOCK_ALLOC(); }#endif return ret;}void s_free (void *ptr){ free(ptr);#if defined(DEBUG_ALLOC) { int pos = 0; LOCK_ALLOC(); while (!s_list_eol(alcl, pos)) { s_alloc_t *tmp = (s_alloc_t *) s_list_get(alcl, pos); if (tmp->addr == ptr) { s_alloc_list_remove(alcl, pos); break; } pos++; } cnt_free++; UNLOCK_ALLOC(); }#endif}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?