Welcome to mirror list, hosted at ThFree Co, Russian Federation.

datastorage.h « memscore « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0eb4cded1b8d23f3a7ceed6893b46d1728b3b712 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
45
46
47
48
49
50
51
52
// memscore - in-memory phrase scoring for Statistical Machine Translation
// Christian Hardmeier, FBK-irst, Trento, 2010
// $Id$

#ifndef DATASTORAGE_H
#define DATASTORAGE_H

#include "memscore.h"

template<class T>
class DataStorage
{
private:
  T *base_;
  size_t pos_;
  size_t chunk_size_;

  DataStorage() : base_(NULL), pos_(0) {
    int ps = getpagesize();
    chunk_size_ = 10 * ps * sizeof(T);
    base_ = new T[chunk_size_];
  }

  DataStorage(const DataStorage &cc) {
    abort();
  }

  ~DataStorage() {}

public:
  static DataStorage &get_instance() {
    static DataStorage<T> instance;
    return instance;
  }

  T *alloc(size_t count) {
    if(count == 0)
      return NULL;

    // The memory leak is intended.
    if(pos_ + count > chunk_size_) {
      base_ = new T[chunk_size_];
      pos_ = 0;
    }

    T *ret = base_ + pos_;
    pos_ += count;
    return ret;
  }
};

#endif