comparison th_ioctx_mem.c @ 692:ea6bcbfb9d18

Comments and cleanup.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Mar 2020 17:59:06 +0200
parents 81d1a3f53b87
children a622d21833e1
comparison
equal deleted inserted replaced
691:49c818acbbbe 692:ea6bcbfb9d18
10 10
11 static BOOL th_mem_realloc(th_ioctx *ctx, const size_t newSize) 11 static BOOL th_mem_realloc(th_ioctx *ctx, const size_t newSize)
12 { 12 {
13 size_t grow; 13 size_t grow;
14 14
15 // Check against max size
15 if (ctx->maxSize > 0 && newSize > ctx->maxSize) 16 if (ctx->maxSize > 0 && newSize > ctx->maxSize)
16 { 17 {
17 ctx->status = THERR_BOUNDS; 18 ctx->status = THERR_BOUNDS;
18 return FALSE; 19 return FALSE;
19 } 20 }
20 21
22 // New size is smaller than old
21 if (newSize < ctx->memAlloc) 23 if (newSize < ctx->memAlloc)
22 return TRUE; 24 goto out;
23 25
26 // Compute the allocation grow amount
24 grow = (ctx->minAlloc > 0) ? ctx->minAlloc : 8 * 1024; 27 grow = (ctx->minAlloc > 0) ? ctx->minAlloc : 8 * 1024;
25 if (newSize - ctx->memAlloc > grow) 28 if (newSize - ctx->memAlloc > grow)
26 grow += newSize - ctx->memAlloc; 29 grow += newSize - ctx->memAlloc;
27 30
28 if (ctx->maxSize > 0 && ctx->memAlloc + grow >= ctx->maxSize) 31 if (ctx->maxSize > 0 && ctx->memAlloc + grow >= ctx->maxSize)
29 { 32 {
30 ctx->status = THERR_BOUNDS; 33 ctx->status = THERR_BOUNDS;
31 return FALSE; 34 return FALSE;
32 } 35 }
33 36
37 // Grow the buffer
34 ctx->memAlloc += grow; 38 ctx->memAlloc += grow;
35 if ((ctx->memData = th_realloc(ctx->memData, ctx->memAlloc)) == NULL) 39 if ((ctx->memData = th_realloc(ctx->memData, ctx->memAlloc)) == NULL)
36 { 40 {
37 ctx->status = THERR_MALLOC; 41 ctx->status = THERR_MALLOC;
38 return FALSE; 42 return FALSE;
39 } 43 }
40 44
45 out:
41 ctx->memSize = newSize; 46 ctx->memSize = newSize;
42 47
43 return TRUE; 48 return TRUE;
44 } 49 }
45 50