免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1898 | 回复: 2
打印 上一主题 下一主题

如何手工实现 Stash的内存分配和管理? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-09 09:53 |只看该作者 |倒序浏览
Stash的结构为:
//Stash.h
#ifndef STASH_H
#define STASH_H

class Stash {
  int size;      // Size of each space
  int quantity;  // Number of storage spaces
  int next;      // Next empty space
  // Dynamically allocated array of bytes:
  unsigned char* storage;
  void inflate(int increase);
public:
  Stash(int size);
  ~Stash();
  int add(void* element);
  void* fetch(int index);
  int count();
};
#endif // STASH_H ///:~

原本Stash的实现为:
//Stash.cpp
#include "Stash2.h"
#include "../require.h"
#include <iostream>;
#include <cassert>;
using namespace std;
const int increment = 100;

Stash::Stash(int sz) {
  size = sz;
  quantity = 0;
  storage = 0;
  next = 0;
}

int Stash::add(void* element) {
  if(next >;= quantity) // Enough space left?
    inflate(increment);
  // Copy element into storage,
  // starting at next empty space:
  int startBytes = next * size;
  unsigned char* e = (unsigned char*)element;
  for(int i = 0; i < size; i++)
    storage[startBytes + i] = e;
  next++;
  return(next - 1); // Index number
}

void* Stash::fetch(int index) {
  require(0 <= index, "Stash::fetch (-)index";
  if(index >;= next)
    return 0; // To indicate the end
  // Produce pointer to desired element:
  return &(storage[index * size]);
}

int Stash::count() {
  return next; // Number of elements in CStash
}

void Stash::inflate(int increase) {
  require(increase >; 0,
    "Stash::inflate zero or negative increase";
  int newQuantity = quantity + increase;
  int newBytes = newQuantity * size;
  int oldBytes = quantity * size;
  unsigned char* b = new unsigned char[newBytes];
  for(int i = 0; i < oldBytes; i++)
    b = storage; // Copy old to new
  delete [](storage); // Old storage
  storage = b; // Point to new memory
  quantity = newQuantity;
}

Stash::~Stash() {
  if(storage != 0) {
   cout << "freeing storage" << endl;
   delete []storage;
  }
} ///:~
现在我编了个内存分配和管理的程序:
//Mem.h
#ifndef MEM_H
#define MEM_H
typedef unsigned char byte;

class Mem
{
        byte* mem;
        int size;
        void ensureMinSize(int minSize);
public:
        Mem();
        Mem(int sz);
        ~Mem();
        int msize();
        byte* pointer();
        byte* pointer(int minSize);
};
#endif

//Mem.cpp
#include "Mem.h"
#include <cstring>;
//using namespace std;

Mem::Mem() { mem = 0; size = 0; }

Mem::Mem(int sz)
{
        mem = 0;
        size = 0;
        ensureMinSize(sz);
}

Mem::~Mem() { delete []mem; }

int Mem::msize() { return size; }

void Mem::ensureMinSize(int minSize)
{
        if(size < minSize)
        {
                byte* newmem = new byte[minSize];
                memset(newmem + size,0,minSize - size);
                memcpy(newmem,mem,size);
                delete []mem;
                mem = newmem;
                size = minSize;
        }
}

byte* Mem::pointer() { return mem; }

byte* Mem::pointer(int minSize)
{
        ensureMinSize(minSize);
        return mem;
}
现在如何用Mem来实现Stash?
其中的require.h文件如下:
//: :require.h
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Test for error conditions in programs
// Local "using namespace std" for old compilers
#ifndef REQUIRE_H
#define REQUIRE_H
#include <cstdio>;
#include <cstdlib>;
#include <fstream>;
#include <string>;

inline void require(bool requirement,
  const std::string& msg = "Requirement failed"{
  using namespace std;
  if (!requirement) {
    fputs(msg.c_str(), stderr);
    fputs("\n", stderr);
    exit(1);
  }
}

inline void requireArgs(int argc, int args,
  const std::string& msg =
    "Must use %d arguments" {
  using namespace std;
   if (argc != args + 1) {
     fprintf(stderr, msg.c_str(), args);
     fputs("\n", stderr);
     exit(1);
   }
}

inline void requireMinArgs(int argc, int minArgs,
  const std::string& msg =
    "Must use at least %d arguments" {
  using namespace std;
  if(argc < minArgs + 1) {
    fprintf(stderr, msg.c_str(), minArgs);
    fputs("\n", stderr);
    exit(1);
  }
}
  
inline void assure(std::ifstream& in,
  const std::string& filename = "" {
  using namespace std;
  if(!in) {
    fprintf(stderr, "Could not open file %s\n",
      filename.c_str());
    exit(1);
  }
}

inline void assure(std:fstream& out,
  const std::string& filename = "" {
  using namespace std;
  if(!out) {
    fprintf(stderr, "Could not open file %s\n",
      filename.c_str());
    exit(1);
  }
}
#endif // REQUIRE_H ///:~

论坛徽章:
0
2 [报告]
发表于 2005-10-09 09:55 |只看该作者

如何手工实现 Stash的内存分配和管理?

哪为朋友能指点下拉,如何用Mem类来实现Stash?

论坛徽章:
0
3 [报告]
发表于 2005-10-09 16:10 |只看该作者

如何手工实现 Stash的内存分配和管理?

是不是我问的问题太痴呆了?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP