MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
allocator.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11#pragma once
12//#include <sys/mman.h> // mmap/munmap
13
14#include "memtracer/common.h"
15#include "memtracer/memtracer.h"
16
17using namespace memtracer;
18#if defined(__linux__)
19#define MT_THROW __THROW
20#else
21#define MT_THROW
22#endif
23
24malloc_func_t orig_malloc = nullptr;
25free_func_t orig_free = nullptr;
26mmap_func_t orig_mmap = nullptr;
27munmap_func_t orig_munmap = nullptr;
28
29extern "C" mt_visible void *malloc(size_t size);
30extern "C" mt_visible void *calloc(size_t nelem, size_t size);
31extern "C" mt_visible void *realloc(void *ptr, size_t size);
32extern "C" mt_visible void free(void *ptr);
33extern "C" mt_visible void cfree(void *ptr);
34extern "C" mt_visible void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
35extern "C" mt_visible int munmap(void *addr, size_t length);
36extern "C" mt_visible char *strdup(const char *s) MT_THROW;
37extern "C" mt_visible char *strndup(const char *s, size_t n) MT_THROW;
38
39mt_visible void *operator new(std::size_t size);
40mt_visible void *operator new[](std::size_t size);
41mt_visible void *operator new(std::size_t size, const std::nothrow_t &) noexcept;
42mt_visible void *operator new[](std::size_t size, const std::nothrow_t &) noexcept;
43mt_visible void operator delete(void *ptr) noexcept;
44mt_visible void operator delete[](void *ptr) noexcept;
45mt_visible void operator delete(void *ptr, const std::nothrow_t &) noexcept;
46mt_visible void operator delete[](void *ptr, const std::nothrow_t &) noexcept;
47mt_visible void operator delete(void *ptr, std::size_t size) noexcept;
48mt_visible void operator delete[](void *ptr, std::size_t size) noexcept;
49
50// unsupported libc functions, for simpler memory tracking.
51extern "C" mt_visible char *realpath(const char *fname, char *resolved_name);
52extern "C" mt_visible void *memalign(size_t alignment, size_t size);
53extern "C" mt_visible void *valloc(size_t size);
54extern "C" mt_visible void *pvalloc(size_t size);
55extern "C" mt_visible int posix_memalign(void **memptr, size_t alignment, size_t size);
56
57#ifdef LINUX
58extern "C" mt_visible int brk(void *addr);
59extern "C" mt_visible void *sbrk(intptr_t increment);
60extern "C" mt_visible long int syscall(long int __sysno, ...);
61#elif defined(__MACH__)
62extern "C" mt_visible void *brk(const void *addr);
63extern "C" mt_visible void *sbrk(int increment);
64extern "C" mt_visible int syscall(int __sysno, ...);
65#endif
66
67// forword libc interface
68#if defined(__GLIBC__) && defined(__linux__)
69extern "C" mt_visible void *__libc_malloc(size_t size) __attribute__((alias("malloc"), used));
70extern "C" mt_visible void *__libc_calloc(size_t nmemb, size_t size) __attribute__((alias("calloc"), used));
71extern "C" mt_visible void *__libc_realloc(void *ptr, size_t size) __attribute__((alias("realloc"), used));
72extern "C" mt_visible void __libc_free(void *ptr) __attribute__((alias("free"), used));
73extern "C" mt_visible void __libc_cfree(void *ptr) __attribute__((alias("cfree"), used));
74extern "C" mt_visible void *__libc_valloc(size_t size) __attribute__((alias("valloc"), used));
75extern "C" mt_visible void *__libc_pvalloc(size_t size) __attribute__((alias("pvalloc"), used));
76extern "C" mt_visible void *__libc_memalign(size_t alignment, size_t size) __attribute__((alias("memalign"), used));
77extern "C" mt_visible int __posix_memalign(void **memptr, size_t alignment, size_t size)
78 __attribute__((alias("posix_memalign"), used));
79#endif