source: tags/ms_r16q2/ARBDB/gb_storage.h

Last change on this file was 9838, checked in by westram, 11 years ago
  • reintegrated branch 'db'
    • GBDATA now is a base class of GBENTRY (new) and GBCONTAINER
      • moved code into classes GBENTRY, GBCONTAINER and GB_MAIN_TYPE
      • several functions now exist 2 (or 3) times (for GBENTRY and GBCONTAINER, and some for GBDATA as well)
      • several functions changed their interface (caller has to explicitely pass GBENTRY or GBCONTAINER)
    • dropped support for ARB DB-version 0
  • adds:
File size: 2.0 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : gb_storage.h                                      //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#ifndef GB_STORAGE_H
12#define GB_STORAGE_H
13
14#ifndef GB_DATA_H
15#include "gb_data.h"
16#endif
17
18inline bool store_inside_entry(int size, int memsize) {
19    // returns true if data can be stored inside GBENTRY
20    return size<256 && memsize<SIZOFINTERN;
21}
22
23inline char *GBENTRY::alloc_data(long Size, long Memsize) {
24    char *mem;
25    gb_assert(!flags2.is_indexed);
26    gb_assert(implicated(stored_external(), !data())); // would leak memory
27
28    if (store_inside_entry(Size, Memsize)) {
29        mark_as_intern();
30
31        info.istr.size    = (unsigned char)Size;
32        info.istr.memsize = (unsigned char)Memsize;
33
34        mem = data();
35    }
36    else {
37        mark_as_extern();
38
39        info.ex.size    = Size;
40        info.ex.memsize = Memsize;
41
42        mem = (char*)gbm_get_mem((size_t)Memsize, GB_GBM_INDEX(this));
43        info.ex.set_data(mem);
44    }
45    return mem;
46}
47
48class GBENTRY_memory : virtual Noncopyable {
49    GBENTRY *gbe;
50    char    *mem;
51public:
52    GBENTRY_memory(GBENTRY *gb_entry, long size, long memsize)
53        : gbe(gb_entry),
54          mem(gbe->alloc_data(size, memsize))
55    {}
56    ~GBENTRY_memory() { gbe->index_re_check_in(); }
57    operator char*() { return mem; }
58};
59
60inline void GBENTRY::insert_data(const char *Data, long Size, long Memsize) {
61    gb_assert(Data);
62    memcpy(GBENTRY_memory(this, Size, Memsize), Data, Memsize);
63}
64
65#else
66#error gb_storage.h included twice
67#endif // GB_STORAGE_H
Note: See TracBrowser for help on using the repository browser.