Browse code

- added shm_available/pkg_available/{qm,fm}_available: return the ammount of free memory

Andrei Pelinescu-Onciul authored on 23/02/2007 23:20:51
Showing 6 changed files
... ...
@@ -38,6 +38,7 @@
38 38
  *              fixed initial size (andrei)
39 39
  *  2006-02-03  fixed realloc out of mem. free bug (andrei)
40 40
  *  2006-04-07  s/DBG/MDBG (andrei)
41
+ *  2007-02-23  added fm_available() (andrei)
41 42
  */
42 43
 
43 44
 
... ...
@@ -601,4 +602,19 @@ void fm_info(struct fm_block* qm, struct mem_info* info)
601 602
 }
602 603
 
603 604
 
605
+
606
+/* returns how much free memory is available
607
+ * on error (not compiled with bookkeeping code) returns (unsigned long)(-1) */
608
+unsigned long fm_available(struct fm_block* qm)
609
+{
610
+
611
+#if defined(DBG_F_MALLOC) || defined(MALLOC_STATS)
612
+	return qm->size-qm->real_used;
613
+#else
614
+	/* we don't know how much free memory we have and it's to expensive
615
+	 * to compute it */
616
+	return ((unsigned long)-1);
617
+#endif
618
+}
619
+
604 620
 #endif
... ...
@@ -139,5 +139,6 @@ void*  fm_realloc(struct fm_block*, void* p, unsigned long size);
139 139
 void  fm_status(struct fm_block*);
140 140
 void  fm_info(struct fm_block*, struct mem_info*);
141 141
 
142
+unsigned long fm_available(struct fm_block*);
142 143
 
143 144
 #endif
... ...
@@ -33,6 +33,7 @@
33 33
  *               (andrei)
34 34
  *  2003-03-07  split init_malloc into init_pkg_mallocs & init_shm_mallocs 
35 35
  *               (andrei)
36
+ *  2007-02-23   added pkg_info() and pkg_available() (andrei)
36 37
  */
37 38
 
38 39
 
... ...
@@ -110,9 +111,13 @@
110 111
 #	ifdef VQ_MALLOC
111 112
 #		define pkg_status()  vqm_status(mem_block)
112 113
 #	elif defined F_MALLOC
113
-#		define pkg_status()  fm_status(mem_block)
114
+#		define pkg_status()    fm_status(mem_block)
115
+#		define pkg_info(mi)    fm_info(mem_block, mi)
116
+#		define pkg_available() fm_available(mem_block)
114 117
 #	else
115
-#		define pkg_status()  qm_status(mem_block)
118
+#		define pkg_status()    qm_status(mem_block)
119
+#		define pkg_info(mi)    qm_info(mem_block, mi)
120
+#		define pkg_available() qm_available(mem_block)
116 121
 #	endif
117 122
 #elif defined(SHM_MEM) && defined(USE_SHM_MEM)
118 123
 #	include "shm_mem.h"
... ...
@@ -39,6 +39,7 @@
39 39
  *              fixed initial size (andrei)
40 40
  *  2006-02-03  fixed realloc out of mem. free bug (andrei)
41 41
  *  2006-04-07  s/DBG/MDBG (andrei)
42
+ *  2007-02-23  added fm_available() (andrei)
42 43
  */
43 44
 
44 45
 
... ...
@@ -714,5 +715,12 @@ void qm_info(struct qm_block* qm, struct mem_info* info)
714 715
 }
715 716
 
716 717
 
718
+/* returns how much free memory is available
719
+ * it never returns an error (unlike fm_available) */
720
+unsigned long qm_available(struct qm_block* qm)
721
+{
722
+	return qm->size-qm->real_used;
723
+}
724
+
717 725
 
718 726
 #endif
... ...
@@ -150,5 +150,6 @@ void* qm_realloc(struct qm_block*, void* p, unsigned long size);
150 150
 void  qm_status(struct qm_block*);
151 151
 void  qm_info(struct qm_block*, struct mem_info*);
152 152
 
153
+unsigned long qm_available(struct qm_block* qm);
153 154
 
154 155
 #endif
... ...
@@ -31,7 +31,8 @@
31 31
  *  2003-06-29  added shm_realloc & replaced shm_resize (andrei)
32 32
  *  2003-11-19  reverted shm_resize to the old version, using
33 33
  *               realloc causes terrible fragmentation  (andrei)
34
- * 2005-03-02   added shm_info() & re-eneabled locking on shm_status (andrei)
34
+ *  2005-03-02   added shm_info() & re-eneabled locking on shm_status (andrei)
35
+ *  2007-02-23   added shm_available() (andrei)
35 36
  */
36 37
 
37 38
 
... ...
@@ -90,6 +91,7 @@
90 91
 #	define MY_STATUS fm_status
91 92
 #	define MY_MEMINFO	fm_info
92 93
 #	define  shm_malloc_init fm_malloc_init
94
+#	define shm_available() fm_available(shm_block)
93 95
 #else
94 96
 #	include "q_malloc.h"
95 97
 	extern struct qm_block* shm_block;
... ...
@@ -99,6 +101,7 @@
99 101
 #	define MY_STATUS qm_status
100 102
 #	define MY_MEMINFO	qm_info
101 103
 #	define  shm_malloc_init qm_malloc_init
104
+#	define shm_available() qm_available(shm_block)
102 105
 #endif
103 106
 
104 107