1 /*-
2  * Copyright (c) 2010, 2020 Oracle and/or its affiliates.  All rights reserved.
3  *
4  * See the file LICENSE for license information.
5  *
6  * $Id$
7  */
8 
9 #include "db_config.h"
10 
11 #include "db_int.h"
12 #include "dbinc/db_page.h"
13 #include "dbinc/heap.h"
14 
15 /*
16  * __heap_db_create --
17  *	Heap specific initialization of the DB structure.
18  *
19  * PUBLIC: int __heap_db_create __P((DB *));
20  */
21 int
__heap_db_create(dbp)22 __heap_db_create(dbp)
23 	DB *dbp;
24 {
25 	HEAP *h;
26 	int ret;
27 
28 	if ((ret = __os_calloc(dbp->env, 1, sizeof(HEAP), &h)) != 0)
29 		return (ret);
30 	dbp->heap_internal = h;
31 	h->region_size = 0;
32 
33 	dbp->get_heapsize = __heap_get_heapsize;
34 	dbp->get_heap_regionsize = __heap_get_heap_regionsize;
35 	dbp->set_heapsize = __heap_set_heapsize;
36 	dbp->set_heap_regionsize = __heap_set_heap_regionsize;
37 
38 	return (0);
39 }
40 
41 /*
42  * __heap_db_close --
43  *      Heap specific discard of the DB structure.
44  *
45  * PUBLIC: int __heap_db_close __P((DB *));
46  */
47 int
__heap_db_close(dbp)48 __heap_db_close(dbp)
49 	DB *dbp;
50 {
51 	HEAP *h;
52 
53 	if ((h = dbp->heap_internal) != NULL) {
54 		__os_free(dbp->env, h);
55 		dbp->heap_internal = NULL;
56 	}
57 	return (0);
58 }
59 
60 /*
61  * __heap_get_heapsize --
62  *	Get the initial size of the heap.
63  *
64  * PUBLIC: int __heap_get_heapsize __P((DB *, u_int32_t *, u_int32_t *));
65  */
66 int
__heap_get_heapsize(dbp,gbytes,bytes)67 __heap_get_heapsize(dbp, gbytes, bytes)
68 	DB *dbp;
69 	u_int32_t *gbytes, *bytes;
70 {
71 	HEAP *h;
72 
73 	DB_ILLEGAL_METHOD(dbp, DB_OK_HEAP);
74 
75 	h = dbp->heap_internal;
76 	*gbytes = h->gbytes;
77 	*bytes = h->bytes;
78 
79 	return (0);
80 }
81 
82 /*
83  * __heap_get_heap_regionsize --
84  *	Get the region size of the heap.
85  *
86  * PUBLIC: int __heap_get_heap_regionsize __P((DB *, u_int32_t *));
87  */
88 int
__heap_get_heap_regionsize(dbp,npages)89 __heap_get_heap_regionsize(dbp, npages)
90 	DB *dbp;
91 	u_int32_t *npages;
92 {
93 	HEAP *h;
94 
95 	DB_ILLEGAL_METHOD(dbp, DB_OK_HEAP);
96 
97 	h = dbp->heap_internal;
98 	*npages = h->region_size;
99 
100 	return (0);
101 }
102 
103 /*
104  * __heap_set_heapsize --
105  *	Set the initial size of the heap.
106  *
107  * PUBLIC: int __heap_set_heapsize __P((DB *, u_int32_t, u_int32_t, u_int32_t));
108  */
109 int
__heap_set_heapsize(dbp,gbytes,bytes,flags)110 __heap_set_heapsize(dbp, gbytes, bytes, flags)
111 	DB *dbp;
112 	u_int32_t gbytes, bytes, flags;
113 {
114 	HEAP *h;
115 
116 	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_heapsize");
117 	DB_ILLEGAL_METHOD(dbp, DB_OK_HEAP);
118 
119 	COMPQUIET(flags, 0);
120 	h = dbp->heap_internal;
121 	h->gbytes = gbytes;
122 	h->bytes = bytes;
123 
124 	return (0);
125 }
126 
127 /*
128  * __heap_set_heap_regionsize --
129  *	Set the region size of the heap.
130  *
131  * PUBLIC: int __heap_set_heap_regionsize __P((DB *, u_int32_t));
132  */
133 int
__heap_set_heap_regionsize(dbp,npages)134 __heap_set_heap_regionsize(dbp, npages)
135 	DB *dbp;
136 	u_int32_t npages;
137 {
138 	HEAP *h;
139 
140 	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_heap_regionsize");
141 	DB_ILLEGAL_METHOD(dbp, DB_OK_HEAP);
142 
143 	if (npages == 0) {
144 		__db_errx(dbp->env, DB_STR("1168", "region size may not be 0"));
145 		return (EINVAL);
146 	}
147 
148 	h = dbp->heap_internal;
149 	h->region_size = npages;
150 
151 	return (0);
152 }
153 
154 /*
155  * __heap_exist --
156  *	Test to see if heap exists or not, used in Perl interface
157  *
158  * PUBLIC: int __heap_exist __P((void));
159  */
160 int
__heap_exist()161 __heap_exist()
162 {
163 	return (1);
164 }
165