root/NEWT0/trunk/src/newt_core/NewtMem.c
| Revision 113, 5.4 kB (checked in by gnue, 17 months ago) |
|---|
| Line | |
|---|---|
| 1 | /*------------------------------------------------------------------------*/ |
| 2 | /** |
| 3 | * @file NewtMem.c |
| 4 | * @brief メモリ管理 |
| 5 | * |
| 6 | * @author M.Nukui |
| 7 | * @date 2003-11-07 |
| 8 | * |
| 9 | * Copyright (C) 2003-2004 M.Nukui All rights reserved. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | /* ヘッダファイル */ |
| 14 | #include <string.h> |
| 15 | #include "NewtMem.h" |
| 16 | |
| 17 | |
| 18 | /*------------------------------------------------------------------------*/ |
| 19 | /** メモリプールの確保 |
| 20 | * |
| 21 | * @param expandspace [in] ブロックの拡張サイズ |
| 22 | * |
| 23 | * @return メモリプール |
| 24 | */ |
| 25 | |
| 26 | newtPool NewtPoolAlloc(int32_t expandspace) |
| 27 | { |
| 28 | newtPool pool; |
| 29 | |
| 30 | pool = (newtPool)calloc(1, sizeof(newtpool_t)); |
| 31 | |
| 32 | if (pool != NULL) |
| 33 | { |
| 34 | pool->maxspace = pool->expandspace = expandspace; |
| 35 | } |
| 36 | |
| 37 | return pool; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | /*------------------------------------------------------------------------*/ |
| 42 | /** メモリプールから指定されたサイズのメモリを確保する |
| 43 | * |
| 44 | * @param pool [in] メモリプール |
| 45 | * @param size [in] データサイズ |
| 46 | * |
| 47 | * @return 確保したメモリへのポインタ |
| 48 | * |
| 49 | * @note 現在はまだ独自メモリ管理は行われていない |
| 50 | */ |
| 51 | |
| 52 | void * NewtMemAlloc(newtPool pool, size_t size) |
| 53 | { |
| 54 | if (pool != NULL) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | return malloc(size); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /*------------------------------------------------------------------------*/ |
| 63 | /** メモリプールから指定されたサイズのメモリを確保する |
| 64 | * |
| 65 | * @param pool [in] メモリプール |
| 66 | * @param number [in] データ数 |
| 67 | * @param size [in] データサイズ |
| 68 | * |
| 69 | * @return 確保したメモリへのポインタ |
| 70 | * |
| 71 | * @note 現在はまだ独自メモリ管理は行われていない |
| 72 | */ |
| 73 | |
| 74 | void * NewtMemCalloc(newtPool pool, size_t number, size_t size) |
| 75 | { |
| 76 | if (pool != NULL) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | return calloc(number, size); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /*------------------------------------------------------------------------*/ |
| 85 | /** メモリプールから指定されたサイズのメモリを再確保する |
| 86 | * |
| 87 | * @param pool [in] メモリプール |
| 88 | * @param ptr [in] メモリへのポインタ |
| 89 | * @param size [in] データサイズ |
| 90 | * |
| 91 | * @return 再確保したメモリへのポインタ |
| 92 | * |
| 93 | * @note 現在はまだ独自メモリ管理は行われていない |
| 94 | */ |
| 95 | |
| 96 | void * NewtMemRealloc(newtPool pool, void * ptr, size_t size) |
| 97 | { |
| 98 | if (pool != NULL) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | return realloc(ptr, size); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /*------------------------------------------------------------------------*/ |
| 107 | /** メモリを解放する |
| 108 | * |
| 109 | * @param ptr [in] メモリへのポインタ |
| 110 | * |
| 111 | * @return なし |
| 112 | * |
| 113 | * @note 現在はまだ独自メモリ管理は行われていない |
| 114 | */ |
| 115 | |
| 116 | void NewtMemFree(void * ptr) |
| 117 | { |
| 118 | free(ptr); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | #if 0 |
| 123 | #pragma mark - |
| 124 | #endif |
| 125 | /*------------------------------------------------------------------------*/ |
| 126 | /** スタック情報をセットアップ |
| 127 | * |
| 128 | * @param stackinfo [out]スタック情報 |
| 129 | * @param pool [in] メモリプール |
| 130 | * @param datasize [in] データサイズ |
| 131 | * @param blocksize [in] ブロックサイズ |
| 132 | * |
| 133 | * @return なし |
| 134 | */ |
| 135 | |
| 136 | void NewtStackSetup(newtStack * stackinfo, |
| 137 | newtPool pool, uint32_t datasize, uint32_t blocksize) |
| 138 | { |
| 139 | memset(stackinfo, 0, sizeof(newtStack)); |
| 140 | |
| 141 | stackinfo->pool = pool; |
| 142 | stackinfo->datasize = datasize; |
| 143 | stackinfo->blocksize = blocksize; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /*------------------------------------------------------------------------*/ |
| 148 | /** スタックを解放 |
| 149 | * |
| 150 | * @param stackinfo [in] スタック情報 |
| 151 | * |
| 152 | * @return なし |
| 153 | */ |
| 154 | |
| 155 | void NewtStackFree(newtStack * stackinfo) |
| 156 | { |
| 157 | if (stackinfo->stackp != NULL) |
| 158 | { |
| 159 | NewtMemFree(stackinfo->stackp); |
| 160 | stackinfo->stackp = NULL; |
| 161 | } |
| 162 | |
| 163 | stackinfo->nums = 0; |
| 164 | stackinfo->sp = 0; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /*------------------------------------------------------------------------*/ |
| 169 | /** スタックを拡張 |
| 170 | * |
| 171 | * @param stackinfo [in] スタック情報 |
| 172 | * @param n [in] 必要とされているスタック長 |
| 173 | * |
| 174 | * @retval true 必要なだけ確保されている |
| 175 | * @retval false 確保できなかった |
| 176 | */ |
| 177 | |
| 178 | bool NewtStackExpand(newtStack * stackinfo, uint32_t n) |
| 179 | { |
| 180 | if (stackinfo->nums < n) |
| 181 | { |
| 182 | uint32_t newsize; |
| 183 | uint32_t nums; |
| 184 | void * newp; |
| 185 | |
| 186 | nums = stackinfo->nums + stackinfo->blocksize; |
| 187 | newsize = stackinfo->datasize * nums; |
| 188 | |
| 189 | newp = NewtMemRealloc(stackinfo->pool, stackinfo->stackp, newsize); |
| 190 | |
| 191 | if (newp == NULL) return false; |
| 192 | |
| 193 | stackinfo->stackp = newp; |
| 194 | stackinfo->nums = nums; |
| 195 | } |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /*------------------------------------------------------------------------*/ |
| 202 | /** スタックをスリム化 |
| 203 | * |
| 204 | * @param stackinfo [in] スタック情報 |
| 205 | * @param n [in] 必要とされているスタック長 |
| 206 | * |
| 207 | * @return なし |
| 208 | */ |
| 209 | |
| 210 | void NewtStackSlim(newtStack * stackinfo, uint32_t n) |
| 211 | { |
| 212 | if (0 < n) |
| 213 | { |
| 214 | uint32_t newsize; |
| 215 | void * newp; |
| 216 | |
| 217 | newsize = stackinfo->datasize * n; |
| 218 | newp = NewtMemRealloc(stackinfo->pool, stackinfo->stackp, newsize); |
| 219 | |
| 220 | if (newp != NULL) |
| 221 | { |
| 222 | stackinfo->stackp = newp; |
| 223 | stackinfo->nums = n; |
| 224 | } |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | NewtStackFree(stackinfo); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
| 233 | #if 0 |
| 234 | #pragma mark - |
| 235 | #endif |
| 236 | /*------------------------------------------------------------------------*/ |
| 237 | /** アラインを計算 |
| 238 | * |
| 239 | * @param n [in] アラインする値 |
| 240 | * @param byte [in] アラインされる単位 |
| 241 | * |
| 242 | * @return アラインされた値 |
| 243 | */ |
| 244 | |
| 245 | uint32_t NewtAlign(uint32_t n, uint16_t byte) |
| 246 | { |
| 247 | uint32_t mod; |
| 248 | |
| 249 | mod = n % byte; |
| 250 | if (0 < mod) n += byte - mod; |
| 251 | |
| 252 | return n; |
| 253 | } |
Note: See TracBrowser
for help on using the browser.
