File: realloc_test.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (197 lines) | stat: -rw-r--r-- 5,761 bytes parent folder | download | duplicates (3)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/common/common.h>

#include <aws/testing/aws_test_harness.h>

#ifdef __MACH__
#    include <CoreFoundation/CoreFoundation.h>
#endif

static size_t s_alloc_counter, s_alloc_total_size, s_call_ct_malloc, s_call_ct_free, s_call_ct_realloc;

static void *s_test_alloc_acquire(struct aws_allocator *allocator, size_t size) {
    (void)allocator;

    s_alloc_counter++;
    s_call_ct_malloc++;
    s_alloc_total_size += size;

    uint8_t *buf = malloc(size + 16);
    *(size_t *)buf = size;
    buf += 16;
    return buf;
}

static void s_test_alloc_release(struct aws_allocator *allocator, void *ptr) {
    (void)allocator;

    uint8_t *buf = ptr;
    s_call_ct_free++;

    buf -= 16;
    size_t old_size = *(size_t *)buf;
    s_alloc_counter--;
    s_alloc_total_size -= old_size;

    free(buf);
}

static size_t s_original_size, s_reported_oldsize;

static void *s_test_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) {
    (void)allocator;

    uint8_t *buf = ptr;
    buf -= 16;
    s_call_ct_realloc++;

    s_original_size = *(size_t *)buf;
    s_reported_oldsize = oldsize;

    /* Always pick a new pointer for test purposes */
    void *newbuf = malloc(newsize);
    if (!newbuf) {
        abort();
    }

    memcpy(newbuf, buf, 16 + (oldsize > newsize ? newsize : oldsize));
    free(buf);
    buf = newbuf;

    *(size_t *)buf = newsize;
    s_alloc_total_size += (newsize - oldsize);

    return buf + 16;
}

static const uint8_t TEST_PATTERN[32] = {0xa5, 0x41, 0xcb, 0xe7, 0x00, 0x19, 0xd9, 0xf3, 0x60, 0x4a, 0x2b,
                                         0x68, 0x55, 0x46, 0xb7, 0xe0, 0x74, 0x91, 0x2a, 0xbe, 0x5e, 0x41,
                                         0x06, 0x39, 0x02, 0x02, 0xf6, 0x79, 0x1c, 0x4a, 0x08, 0xa9};

AWS_TEST_CASE(test_realloc_fallback, s_test_realloc_fallback_fn)
static int s_test_realloc_fallback_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    struct aws_allocator test_allocator = {
        .mem_acquire = s_test_alloc_acquire,
        .mem_release = s_test_alloc_release,
        .mem_realloc = NULL,
    };

    s_call_ct_malloc = s_call_ct_free = s_call_ct_realloc = 0;

    void *buf = aws_mem_acquire(&test_allocator, 32);
    void *oldbuf = buf;
    memcpy(buf, TEST_PATTERN, 32);
    ASSERT_SUCCESS(aws_mem_realloc(&test_allocator, &buf, 32, 64));
    ASSERT_INT_EQUALS(s_call_ct_malloc, 2);
    ASSERT_INT_EQUALS(s_call_ct_free, 1);
    ASSERT_INT_EQUALS(s_alloc_counter, 1);
    ASSERT_INT_EQUALS(s_alloc_total_size, 64);
    ASSERT_INT_EQUALS(memcmp(buf, TEST_PATTERN, 32), 0);
    ASSERT_FALSE(buf == oldbuf);

    aws_mem_release(&test_allocator, buf);

    return 0;
}

AWS_TEST_CASE(test_realloc_passthrough, s_test_realloc_passthrough_fn)
static int s_test_realloc_passthrough_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    struct aws_allocator test_allocator = {
        .mem_acquire = s_test_alloc_acquire,
        .mem_release = s_test_alloc_release,
        .mem_realloc = s_test_realloc,
    };

    s_call_ct_malloc = s_call_ct_free = s_call_ct_realloc = 0;

    void *buf = aws_mem_acquire(&test_allocator, 32);
    void *oldbuf = buf;
    memcpy(buf, TEST_PATTERN, 32);

    ASSERT_SUCCESS(aws_mem_realloc(&test_allocator, &buf, 32, 64));
    ASSERT_INT_EQUALS(memcmp(buf, TEST_PATTERN, 32), 0);
    ASSERT_INT_EQUALS(s_reported_oldsize, 32);
    ASSERT_INT_EQUALS(s_original_size, 32);
    ASSERT_INT_EQUALS(s_call_ct_malloc, 1);
    ASSERT_INT_EQUALS(s_call_ct_free, 0);
    ASSERT_FALSE(buf == oldbuf);

    aws_mem_release(&test_allocator, buf);

    return 0;
}

AWS_TEST_CASE(test_cf_allocator_wrapper, s_test_cf_allocator_wrapper_fn)

static int s_test_cf_allocator_wrapper_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

#ifdef __MACH__
    CFAllocatorRef cf_allocator = aws_wrapped_cf_allocator_new(allocator);
    ASSERT_NOT_NULL(cf_allocator);
    char test_prefix[] = "test_string";
    CFStringRef test_str = CFStringCreateWithCString(cf_allocator, test_prefix, kCFStringEncodingUTF8);

    ASSERT_NOT_NULL(test_str);
    /* NOLINTNEXTLINE */
    ASSERT_BIN_ARRAYS_EQUALS(
        test_prefix,
        sizeof(test_prefix) - 1,
        CFStringGetCStringPtr(test_str, kCFStringEncodingUTF8),
        CFStringGetLength(test_str));

    CFRelease(test_str);
    aws_wrapped_cf_allocator_destroy(cf_allocator);
#endif

    return 0;
}

AWS_TEST_CASE(test_acquire_many, s_test_acquire_many_fn)
static int s_test_acquire_many_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    void *a = NULL;
    void *b = NULL;
    void *buffer = aws_mem_acquire_many(allocator, 2, &a, (size_t)64, &b, (size_t)64);

    ASSERT_NOT_NULL(buffer);
    ASSERT_NOT_NULL(a);
    ASSERT_NOT_NULL(b);

    ASSERT_UINT_EQUALS((uintptr_t)a, (uintptr_t)buffer);
    ASSERT_UINT_EQUALS((uintptr_t)b, (uintptr_t)buffer + 64);
    ASSERT_UINT_EQUALS((uintptr_t)a % sizeof(intmax_t), 0);
    ASSERT_UINT_EQUALS((uintptr_t)b % sizeof(intmax_t), 0);

    aws_mem_release(allocator, buffer);
    a = NULL;
    b = NULL;

    buffer = aws_mem_acquire_many(allocator, 2, &a, (size_t)1, &b, (size_t)1);

    ASSERT_NOT_NULL(buffer);
    ASSERT_NOT_NULL(a);
    ASSERT_NOT_NULL(b);

    ASSERT_UINT_EQUALS((uintptr_t)a, (uintptr_t)buffer);
    ASSERT_UINT_EQUALS((uintptr_t)b, (uintptr_t)buffer + sizeof(intmax_t));
    ASSERT_UINT_EQUALS((uintptr_t)a % sizeof(intmax_t), 0);
    ASSERT_UINT_EQUALS((uintptr_t)b % sizeof(intmax_t), 0);

    aws_mem_release(allocator, buffer);

    return 0;
}