xmalloc.c (b3e7694832e81d7a904a10f525f8797b753bf0d3) xmalloc.c (feaae6ba1ace0091384ac371423976cd15e59e5a)
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright 1996-1998 John D. Polstra.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 61 unchanged lines hidden (view full) ---

70
71 len = strlen(str) + 1;
72 copy = xmalloc(len);
73 memcpy(copy, str, len);
74 return (copy);
75}
76
77void *
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright 1996-1998 John D. Polstra.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 61 unchanged lines hidden (view full) ---

70
71 len = strlen(str) + 1;
72 copy = xmalloc(len);
73 memcpy(copy, str, len);
74 return (copy);
75}
76
77void *
78malloc_aligned(size_t size, size_t align, size_t offset)
78xmalloc_aligned(size_t size, size_t align, size_t offset)
79{
79{
80 char *mem, *res;
81 uintptr_t x;
80 void *res;
82
83 offset &= align - 1;
84 if (align < sizeof(void *))
85 align = sizeof(void *);
86
81
82 offset &= align - 1;
83 if (align < sizeof(void *))
84 align = sizeof(void *);
85
87 mem = xmalloc(size + 3 * align + offset);
88 x = roundup((uintptr_t)mem + sizeof(void *), align);
89 x += offset;
90 res = (void *)x;
91 x -= sizeof(void *);
92 memcpy((void *)x, &mem, sizeof(mem));
86 res = __crt_aligned_alloc_offset(align, size, offset);
87 if (res == NULL) {
88 rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
89 _exit(1);
90 }
93 return (res);
94}
91 return (res);
92}
95
96void
97free_aligned(void *ptr)
98{
99 void *mem;
100 uintptr_t x;
101
102 if (ptr == NULL)
103 return;
104 x = (uintptr_t)ptr;
105 x -= sizeof(void *);
106 memcpy(&mem, (void *)x, sizeof(mem));
107 free(mem);
108}