设为首页 加入收藏

TOP

数据结构内存管理c++实现(一)
2014-04-06 17:35:10 来源: 作者: 【 】 浏览:328
Tags:数据结构 内存 管理 实现

  内存管理边界标识首次拟合c++实现,持续更新中,添加测试结果,修改Bug

  ///

  /// Copyright (c) 2014 xseekerj, All Rights Reserved.

  ///

  #include <stdlib.h>

  #include <stdio.h>

  #include <time.h>

  #include <math.h>

  #include <stdint.h>

  #include <string.h>

  #include <mutex>

  #define nullptr 0

  inline int rrand(int min, int max)

  {

  return ( rand() % ((max) - (min)) + (min) );

  }

  /*

  * size in bytes constant: SZ Definition

  */

  #ifndef _POL_SZ

  #define _POL_SZ

  static const uint64_t __B__ =  (1);

  static const uint64_t __KB__ = (1024 * __B__) ;

  static const uint64_t __MB__ = (1024 * __KB__);

  static const uint64_t __GB__ = (1024 * __MB__);

  static const uint64_t __TB__ = (1024 * __GB__);

  static const uint64_t __PB__ = (1024 * __TB__);

  static const uint64_t __EB__ = (1024 * __PB__);

  #define __b__ __B__

  #define __k__ __K__

  #define __m__ __M__

  #define __g__ __G__

  #define __t__ __T__

  #define __e__ __E__

  #define __K__ __KB__

  #define __M__ __MB__

  #define __G__ __GB__

  #define __T__ __TB__

  #define __P__ __PB__

  #define __E__ __EB__

  #define SZ(n, u) ( (n) * __##u##__ )

  static const uint8_t __MAX_UINT8 = SZ(256, B) - 1;

  static const uint16_t __MAX_UINT16 = SZ(64, KB) - 1;

  static const uint32_t __MAX_UINT32 = SZ(4, GB) - 1;

  static const uint64_t __MAX_UINT64 = SZ(15, EB) + (SZ(1, EB) - 1);

  #endif   /* _POL_SZ */

  #define sz_align(d,a) (((d) + (a - 1)) & ~(a - 1))

  // #define EANBLE_MULTI_THREAD 1

  class mempool

  {

  struct memblock_head_t

  {

  memblock_head_t* prev;

  memblock_head_t* next;

  size_t           size;

  bool             unused;

  };

  struct memblock_foot_t

  {

  memblock_head_t* uplink;

  };

  static const size_t reserved_size = sizeof(memblock_head_t) + sizeof(memblock_foot_t);

  static const size_t min_size = sizeof(void*) * 2;

  static const size_t min_block_size = reserved_size + min_size;

  #define FOOT_LOC(h) ((memblock_foot_t*)( (char*)h + h->size - sizeof(memblock_foot_t) ) )

  #define HEAD_LOC(pu) ( (memblock_head_t*)( (char*)pu - sizeof(memblock_head_t) ) )

  #define UPDATE_FOOT(h) ( FOOT_LOC(h)->uplink = h )

  #define LEFT_FOOT_LOC(h) ( (memblock_foot_t*)( (char*)h - sizeof(memblock_foot_t) ) )

  #define RIGHT_HEAD_LOC(h) ( (memblock_head_t*)( (char*)h + h->size ) )

  #define REMAIN_SIZE(blk,n) (blk->size - reserved_size - (n) ) // calculate remain size after n bytes allocated.

  public:

  mempool(size_t size = SZ(64, k))

  {

  init(size);

  }

  ~mempool(void)

  {

  free(memory_);

  }

  void init(size_t size)

  {

  size = sz_align(size, SZ(64, k));

  memory_ = malloc(size);

  memblock_head_t* header = (memblock_head_t*)(memory_);

  header->size = size;

  header->unused = true;

  header->prev = header;

  header->next = header;

  this->freelist_ = FOOT_LOC(header)->uplink = header;

  this->memory_size_ = size;

  }

  void* allocate(size_t size)

  {

  #ifdef EANBLE_MULTI_THREAD

  std::unique_lock<std::mutex> guard(this->mtx_);

  #endif

  size_t n = sz_align(size, min_size);

  memblock_head_t* blk = freelist_;

  for(; blk != nullptr &&

  blk->size - reserved_size < n &&

  blk->next != freelist_; blk = blk->next){;}

  if(blk == nullptr || blk->size - reserved_size < n) {

  return malloc(size);

  }

  else {

  blk->unused = false;

  char* p = (char*)blk + sizeof(memblock_head_t);

  if( REMAIN_SIZE(blk, n) < min_block_size ) /* can't construct the new memory block to respond any alloc request. */

  {

  // blk->size =

  if(blk == freelist_) {

  freelist_ = nullptr;

  }

  else {

  freelist_->prev = blk->next; blk->next->prev = freelist_;

  }

  }

  else {

  /* remain block */

  memblock_head_t* reblock = (memblock_head_t*)(p + n + sizeof(memblock_foot_t));

  reblock->unused = true;

  reblock->size = (char*)blk + blk->size - (char*)reblock;

  UPDATE_FOOT(reblock);

  blk->size = (char*)reblock - (char*)blk;

  UPDATE_FOOT(blk);

  if(blk->next == blk)

  { // self

  blk->next = reblock;

  blk->prev = reblock;

  reblock->prev = blk;

  reblock->next = blk;

  }

  else { // insert

  reblock->prev = blk;

  reblock->next = blk->next;

  blk->next->prev = reblock;

  blk->next = reblock;

  // ptr->prev = reblock;

  }

     

首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用BOOST BIND库提高C++性能 下一篇C++写的socket网络爬虫

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)
·Redis配置中`require (2025-12-26 03:18:58)
·Asus Armoury Crate (2025-12-26 02:52:33)
·WindowsFX (LinuxFX) (2025-12-26 02:52:30)