/src/ftk_allocator_profile.c
C | 134 lines | 84 code | 21 blank | 29 comment | 12 complexity | 0c0464c5d352862e6f742921b2c47683 MD5 | raw file
1/* 2 * File: ftk_allocator.c 3 * Author: Li XianJing <xianjimli@hotmail.com> 4 * Brief: memory allocator interface. 5 * 6 * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com> 7 * 8 * Licensed under the Academic Free License version 2.1 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 */ 24 25/* 26 * History: 27 * ================================================================ 28 * 2010-01-31 Li XianJing <xianjimli@hotmail.com> created 29 * 30 */ 31 32#include "ftk.h" 33#include "ftk_allocator_profile.h" 34 35typedef struct _AllocPrivInfo 36{ 37 int free_times; 38 int allocated_times; 39 int allocated; 40 int max_allocated; 41 int total_allocated; 42 FtkAllocator* allocator; 43}PrivInfo; 44 45void* ftk_allocator_profile_alloc(FtkAllocator* thiz, size_t size) 46{ 47 DECL_PRIV(thiz, priv); 48 void* ptr = ftk_allocator_alloc(priv->allocator, size + sizeof(size_t)); 49 50 if(ptr != NULL) 51 { 52 priv->allocated_times++; 53 priv->allocated += size; 54 priv->total_allocated += size; 55 priv->max_allocated = priv->allocated > priv->max_allocated? priv->allocated : priv->max_allocated; 56 57 *(size_t*)ptr = size; 58 ptr = (char*)ptr + sizeof(size_t); 59 } 60 61 return ptr; 62} 63 64void* ftk_allocator_profile_realloc(FtkAllocator* thiz, void* ptr, size_t new_size) 65{ 66 size_t old_size = 0; 67 DECL_PRIV(thiz, priv); 68 69 ptr = ptr != NULL ? (char*)ptr - sizeof(size_t) : NULL; 70 old_size = ptr != NULL ? *(size_t*)ptr : 0; 71 72 ptr = ftk_allocator_realloc(priv->allocator, ptr, new_size + sizeof(size_t)); 73 if(ptr != NULL) 74 { 75 priv->allocated += new_size - old_size; 76 priv->total_allocated += new_size; 77 priv->max_allocated = priv->allocated > priv->max_allocated? priv->allocated : priv->max_allocated; 78 *(size_t*)ptr = new_size; 79 ptr = (char*)ptr + sizeof(size_t); 80 } 81 82 return ptr; 83} 84 85void ftk_allocator_profile_free(FtkAllocator* thiz, void* ptr) 86{ 87 size_t size = 0; 88 DECL_PRIV(thiz, priv); 89 90 ptr = ptr != NULL ? (char*)ptr - sizeof(size_t) : NULL; 91 size = ptr != NULL ? *(size_t*)ptr : 0; 92 if(ptr != NULL) 93 { 94 priv->free_times++; 95 priv->allocated -= size; 96 } 97 ftk_allocator_free(priv->allocator, ptr); 98 99 return; 100} 101 102void ftk_allocator_profile_destroy(FtkAllocator* thiz) 103{ 104 DECL_PRIV(thiz, priv); 105 FtkAllocator* allocator = priv->allocator; 106 107 ftk_logd("%s: allocated_times=%d\n", __func__, priv->allocated_times); 108 ftk_logd("%s: free_times=%d\n", __func__, priv->free_times); 109 ftk_logd("%s: allocated=%d\n", __func__, priv->allocated); 110 ftk_logd("%s: max_allocated=%d\n", __func__, priv->max_allocated); 111 ftk_logd("%s: total_allocated=%d\n", __func__, priv->total_allocated); 112 ftk_allocator_free(allocator, thiz); 113 ftk_allocator_destroy(allocator); 114 115 return; 116} 117 118FtkAllocator* ftk_allocator_profile_create(FtkAllocator* allocator) 119{ 120 FtkAllocator* thiz = (FtkAllocator*)ftk_allocator_zalloc(allocator, sizeof(FtkAllocator) + sizeof(PrivInfo)); 121 122 if(thiz != NULL) 123 { 124 DECL_PRIV(thiz, priv); 125 priv->allocator = allocator; 126 127 thiz->alloc = ftk_allocator_profile_alloc; 128 thiz->realloc = ftk_allocator_profile_realloc; 129 thiz->free = ftk_allocator_profile_free; 130 thiz->destroy = ftk_allocator_profile_destroy; 131 } 132 133 return thiz; 134}