sanitizer_allocator.cc revision 8d4ca28e8dc31036dd5bcda82ae97d30d5b9c631
1//===-- sanitizer_allocator.cc --------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries.
12// This allocator that is used inside run-times.
13//===----------------------------------------------------------------------===//
14#include "sanitizer_common.h"
15
16// FIXME: We should probably use more low-level allocator that would
17// mmap some pages and split them into chunks to fulfill requests.
18#include <stdlib.h>
19
20namespace __sanitizer {
21
22static const u64 kInternalAllocBlockMagic = 0x7A6CB03ABCEBC042ull;
23
24void *InternalAlloc(uptr size) {
25  void *p = malloc(size + sizeof(u64));
26  ((u64*)p)[0] = kInternalAllocBlockMagic;
27  return (char*)p + sizeof(u64);
28}
29
30void InternalFree(void *addr) {
31  if (!addr) return;
32  addr = (char*)addr - sizeof(u64);
33  CHECK_EQ(((u64*)addr)[0], kInternalAllocBlockMagic);
34  ((u64*)addr)[0] = 0;
35  free(addr);
36}
37
38}  // namespace __sanitizer
39