mem.h 3.6 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2010
//

羽飞's avatar
羽飞 已提交
15
#pragma once
羽飞's avatar
羽飞 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

#include <stdlib.h>
#include <string.h>

#include <new>

#include "common/defs.h"
namespace common {

#ifndef MEM_DEBUG
#define lcalloc calloc
#define lmalloc malloc
#define lfree free
#define lrealloc realloc

#else

typedef struct MemID_t {
public:
  const static int MEM_FILENAME_LEN = 32;
  struct MemID_t *mNext;
  char mFile[MEM_FILENAME_LEN];
  u64_t mSize;
  u32_t mLine;
} MemID;

class CLMemTrace {
public:
44
  static void *malloc(size_t size, const char *file, const int line, bool retry = false) throw(std::bad_alloc);
羽飞's avatar
羽飞 已提交
45 46

  // just use for realloc, same functionality as realloc
47
  static void *realloc(void *ptr, size_t size, const char *file, const int line);
羽飞's avatar
羽飞 已提交
48 49 50 51 52 53 54 55

  static void free(void *ptr);

  static void output();

  /**
   * set whether show every details
   */
56 57 58 59
  static void setVerbose(bool verbose)
  {
    mVerbose = verbose;
  }
羽飞's avatar
羽飞 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

protected:
  static std::new_handler getNewHandler();

  /**
   * Don't use stl due to map or string will use new,
   * so it will leading to dead loop
   */
  // static std::map<void *, CMemID> mMemIds;
  const static int MEM_HASHTABLE_SIZE = 16384;

  static MemID *mMemIDs[MEM_HASHTABLE_SIZE];
  static u64_t mUsedSize;
  static pthread_mutex_t mMutex;
  static bool mVerbose;
};

#define lcalloc(nmemb, size) Lcalloc(nmemb, size, __FILE__, __LINE__)

#define lmalloc(size) Lmalloc(size, __FILE__, __LINE__)

#define lrealloc(ptr, size) Lrealloc(ptr, size, __FILE__, __LINE__)

#define lfree(ptr) Lfree(ptr)

void *Lcalloc(size_t nmemb, size_t size, const char *file, const int line);
void *Lmalloc(size_t size, const char *file, const int line);
void Lfree(void *ptr);
void *Lrealloc(void *ptr, size_t size, const char *file, const int line);

/**
 * My own operator new function, it will record alloc information
 * default alloc memory or DEBUG_NEW will go to this function
 */
static void *operator new(std::size_t size, const char *file, int line);
static void *operator new[](std::size_t size, const char *file, int line);

/**
 * when user use debug_new, then it will go to this function
 */
static void *operator new(std::size_t size) throw(std::bad_alloc);
static void *operator new[](std::size_t size) throw(std::bad_alloc);

/**
 * when user use nothrow 's new function, it will go this function
 */
static void *operator new(std::size_t size, const std::nothrow_t &) throw();
static void *operator new[](std::size_t size, const std::nothrow_t &) throw();

/**
 * my own operator function, low level function
 */
static void operator delete(void *pointer, const char *file, int line);
static void operator delete[](void *pointer, const char *file, int line);

/**
 * the default delete function
 */
static void operator delete(void *pointer);
static void operator delete[](void *pointer);

///**
// * nothrow 's delete function, it will be the pair of nothrow's new function
// */
// void operator delete(void* pointer, const std::nothrow_t&);
// void operator delete[](void* pointer, const std::nothrow_t&);

#define new DEBUG_NEW
#define DEBUG_NEW new (__FILE__, __LINE__)
#define debug_new new

#endif /* MEM_DEBUG */

133
}  // namespace common