1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#include "alloc.h"
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
/* Sequence-capacity growth policy for the forward-pass workspaces:
* For new growth:
* - never allocate less than the minimum required size.
* - round up to the granularity so small length changes do not trigger a realloc on every call.
* - keep existing capacity if it is already larger. */
#define LANGDET_MIN_WORKSPACE_SEQ_CAP 16
#define LANGDET_WORKSPACE_SEQ_GRANULARITY 16
int mul_size(size_t a, size_t b, size_t *out) {
if (a != 0 && b > SIZE_MAX / a)
return -1;
*out = a * b;
return 0;
}
int grow_cap(int current, int needed, int *out) {
if (current < 0 || needed <= 0)
return -1;
int cap = needed;
if (cap < LANGDET_MIN_WORKSPACE_SEQ_CAP)
cap = LANGDET_MIN_WORKSPACE_SEQ_CAP;
int rem = cap % LANGDET_WORKSPACE_SEQ_GRANULARITY;
if (rem != 0) {
int pad = LANGDET_WORKSPACE_SEQ_GRANULARITY - rem;
if (cap > INT_MAX - pad)
return -1;
cap += pad;
}
if (cap < current)
cap = current;
*out = cap;
return 0;
}
int realloc_floats(float **ptr, size_t count) {
size_t bytes;
if (mul_size(count, sizeof(float), &bytes) != 0)
return -1;
void *p = realloc(*ptr, bytes);
if (!p && bytes != 0)
return -1;
*ptr = (float *)p;
return 0;
}
int realloc_floats_2d(float **ptr, int rows, int cols) {
if (rows < 0 || cols < 0)
return -1;
size_t count;
if (mul_size((size_t)rows, (size_t)cols, &count) != 0)
return -1;
return realloc_floats(ptr, count);
}
|