Debian Codegraph @

crt/s2n/tests/sidetrail/working/stubs/s2n_mem.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <stdint.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <sys/mman.h>
20
21 #include "error/s2n_errno.h"
22
23 #include "utils/s2n_blob.h"
24 #include "utils/s2n_mem.h"
25 #include "utils/s2n_safety.h"
26
27 static long page_size = 4096;
28
29 int s2n_mem_init(void)
30 {
31 POSIX_GUARD(page_size = sysconf(_SC_PAGESIZE));
32
33 return 0;
34 }
35
36 int s2n_mem_cleanup(void)
37 {
38 page_size = 4096;
39 return 0;
40 }
41
42 int s2n_alloc(struct s2n_blob *b, uint32_t size)
43 {
44 b->data = NULL;
45 b->size = 0;
46 b->allocated = 0;
47 POSIX_GUARD(s2n_realloc(b, size));
48 return 0;
49 }
50
51 void *realloc( void *ptr, size_t new_size )
52 {
53 /* just leave it undet for now */
54 void* ret = malloc(new_size);
55 return ret;
56 }
57
58 int s2n_realloc(struct s2n_blob *b, uint32_t size)
59 {
60 /* blob already has space for the request */
61 if (size < b->allocated) {
62 b->size = size;
63 return 0;
64 }
65
66 void *data = realloc(b->data, size);
67 if (!data) {
68 POSIX_BAIL(S2N_ERR_ALLOC);
69 }
70
71 b->data = data;
72 b->size = size;
73 b->allocated = size;
74 return 0;
75 }
76
77 int s2n_free(struct s2n_blob *b)
78 {
79 free(b->data);
80 b->data = NULL;
81 b->size = 0;
82 b->allocated = 0;
83
84 return 0;
85 }
86
87 int s2n_dup(struct s2n_blob *from, struct s2n_blob *to)
88 {
89 POSIX_ENSURE_EQ(to->size, 0);
90 POSIX_ENSURE_EQ(to->data, NULL);
91
92 POSIX_GUARD(s2n_alloc(to, from->size));
93
94 POSIX_CHECKED_MEMCPY(to->data, from->data, to->size);
95
96 return 0;
97 }