xmalloc.c (6a068746777241722b2b32c5d0bc443a2a64d80b) | xmalloc.c (dfe296c43a26175f98ded41168e5c106edd52599) |
---|---|
1/*- 2 * Copyright 1996-1998 John D. Polstra. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 53 unchanged lines hidden (view full) --- 62 char *copy; 63 size_t len; 64 65 len = strlen(str) + 1; 66 copy = xmalloc(len); 67 memcpy(copy, str, len); 68 return (copy); 69} | 1/*- 2 * Copyright 1996-1998 John D. Polstra. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 53 unchanged lines hidden (view full) --- 62 char *copy; 63 size_t len; 64 65 len = strlen(str) + 1; 66 copy = xmalloc(len); 67 memcpy(copy, str, len); 68 return (copy); 69} |
70 71void * 72malloc_aligned(size_t size, size_t align) 73{ 74 void *mem, *res; 75 uintptr_t x; 76 size_t asize, r; 77 78 r = round(sizeof(void *), align); 79 asize = round(size, align) + r; 80 mem = xmalloc(asize); 81 x = (uintptr_t)mem; 82 res = (void *)round(x, align); 83 *(void **)((uintptr_t)res - sizeof(void *)) = mem; 84 return (res); 85} 86 87void 88free_aligned(void *ptr) 89{ 90 void *mem; 91 uintptr_t x; 92 93 if (ptr == NULL) 94 return; 95 x = (uintptr_t)ptr; 96 x -= sizeof(void *); 97 mem = *(void **)x; 98 free(mem); 99} |
|