dtinfo: Fix coverity warning related to using delete not delete []
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / storage / page_storage.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $XConsortium: page_storage.h /main/7 1996/07/18 14:55:30 drk $
25  *
26  * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51 #ifndef _page_storage_h
52 #define _page_storage_h 1
53
54 #define NUM_PAGES 10
55
56 #include "utility/pm_random.h"
57 #include "dstr/heap.h"
58 #include "dstr/void_ptr_array.h"
59 #include "dynhash/imp_die.h"
60 #include "storage/unixf_storage.h"
61 #include "storage/heap_comp_funcs.h"
62 #include "storage/page_cache.h"
63 #include "storage/spointer.h"
64
65 enum direction { positive, negative };
66
67 class page;
68
69 class str_index_record_t : public root
70 {
71
72 public:
73    int str_offset;
74    mmdb_pos_t loc;
75
76    str_index_record_t(int offset, mmdb_pos_t lc = 0);
77    virtual ~str_index_record_t() {};
78 };
79
80 void delete_str_index_record(const void* str_index_record_ptr);
81
82 typedef str_index_record_t* str_index_record_tPtr;
83
84 ////////////////////////////////////////////
85 ////////////////////////////////////////////
86 class store_trans
87 {
88
89 protected:
90    enum trans_t { ENABLED, DISABLED };
91
92    char* path;
93    char* name;
94
95    trans_t status;
96    unixf_storage* log_store;
97    imp_die* log_index;
98    int max_pages;
99    int page_sz;
100
101 public:
102    store_trans(char* path, char*name, int page_sz); 
103    ~store_trans();
104
105    void init(rep_policy*);
106    void quit();
107
108    void set_max_pages(int max_pgs) {
109       max_pages = max_pgs;
110    };
111
112    friend class page_storage;
113 };
114
115 /******************************************************/
116 // cached page storage. Implemented on Unix file class.
117 /******************************************************/
118
119 #ifdef C_API
120 #define f_global_pcache (*f_global_pcache_ptr)
121 #endif
122
123
124 class page_storage : public abs_storage 
125 {
126
127 protected:
128    store_trans trans_info;
129
130    int page_sz ;           // page size
131
132    static int dv_sz ;
133    static int abs_off ;
134
135 #ifndef C_API
136    static page_cache_global_part f_global_pcache;
137 #else
138    static page_cache_global_part* f_global_pcache_ptr;
139 #endif
140
141    page_cache_local_part f_local_pcache;
142
143    int total_pages;
144
145 // byte order 
146    int v_server_order;
147    int v_db_order;
148
149    buffer* v_buf;          // aux. buf.
150
151    int pagings ;
152    int total_page_access;
153
154 protected:
155
156    Boolean seek_loc_negative(mmdb_pos_t& loc, int smd);
157    Boolean seek_loc_positive(mmdb_pos_t& loc, int smd);
158
159 public:
160    page_storage(char* path, char* name,
161                 unixf_storage* store, 
162                 int page_sz = PAGSIZ, 
163                 int num_cached_pages = NUM_PAGES,
164                 mmdb_byte_order_t db_order_when_create_store = mmdb_big_endian
165                );
166    virtual ~page_storage();
167
168    void remove(); // remove all pages in the store
169
170    void sync();
171    void sync(int pagenum);
172    void sync(page*);
173
174    void begin_trans();
175    void commit_trans();
176    void roll_back();
177    void save_to_log(page* page_ptr);
178
179 // get server and db order
180    int server_order() { return v_server_order; } ;
181    int db_order() { return v_db_order; };
182
183
184 // i/o functions
185    int readString (mmdb_pos_t loc, char* base, int len, int str_offset = 0); 
186    int get_str_ptr(mmdb_pos_t loc, char*&, int& len) ;
187
188    int updateString(mmdb_pos_t loc, const char* base, int len, int string_ofst = 0, Boolean flush = false);
189    int deleteString (mmdb_pos_t loc, Boolean flush = false);
190    int insertString(mmdb_pos_t& loc, const char* base, int len, Boolean flush = false);
191    int allocString (mmdb_pos_t& loc, int len, char*&, int mode = 0);
192    int appendString(mmdb_pos_t loc, const char*, int len, Boolean flush_opt = false);
193
194    int set_page_dirty(mmdb_pos_t loc);
195
196 // iteration functions
197    typedef enum access { READ, WRITE } access_t;
198    int first() const;             // first page's index in the store
199    page* operator()(int page_num, enum access intent) ;    //get the page
200    void next(int&) const;         // next page's index. 
201
202
203 // format the store to contain extra empty 'pages' pages
204    int add_page_frames(int pages);   
205
206
207 // get locs of the pieces that a long string is broken into
208 // The array should be deleted after use.
209    int get_str_locs(mmdb_pos_t str_loc, str_index_record_tPtr*& locs, int& vector_leng);
210
211 // seek loc to (loc +/- 1) position 
212    Boolean seek_loc(mmdb_pos_t& loc, const direction = positive, int = spointer_t::FIRST_RECD );
213    mmdb_pos_t first_loc(); // return first loc in the store
214    mmdb_pos_t last_loc(); // return last loc in the store
215
216 // store status query functions
217    int page_size() { return page_sz; }; 
218
219 //
220    void set_page_size(int pgsz) { page_sz = pgsz; }; 
221
222 // how many pages in total in the store
223    int pages() const { return total_pages; }; 
224
225 // paging counting function
226    void reset_paging_count();
227    int paging_count() const;
228
229    Boolean io_mode(int mode) ;
230
231 // return an aux. buf.
232    buffer& aux_buf();
233
234 // printing functions
235    virtual io_status asciiOut(ostream&) ;
236  
237    friend void close_file(const void*);
238    friend void remove_from_global_cache(const void*);
239
240    friend class storage_mgr_t;
241    friend class handler;
242    friend class page_cache_local_part;
243    friend class page_cache_global_part;
244
245 #ifdef C_API
246    friend void initialize_MMDB();
247    friend void quit_MMDB();
248 #endif
249 };
250
251 typedef page_storage* page_storagePtr;
252
253 #endif