Commits

Anonymous committed bafa696

Unit tests for scratch allocator.

Comments (0)

Files changed (2)

 
 			// Mark this slot as free
 			Header *h = header(p);
-			assert(h->size & 0x80000000u == 0);
+			assert((h->size & 0x80000000u) == 0);
 			h->size = h->size | 0x80000000u;
 
 			// Advance the free pointer past all free slots.
 			while (_free != _allocate) {
 				Header *h = (Header *)_free;
-				if (h->size & 0x80000000u == 0)
+				if ((h->size & 0x80000000u) == 0)
 					break;
 
 				_free += h->size & 0x7fffffffu;
 
 		virtual uint32_t allocated_size(void *p) {
 			Header *h = header(p);
-			return h->size;
+			return h->size - ((char *)p - (char *)h);
 		}
 
 		virtual uint32_t total_allocated() {
 
 		memory_globals::shutdown();
 	}
+
+	void test_scratch() {
+		memory_globals::init(256*1024);
+		Allocator &a = memory_globals::default_scratch_allocator();
+
+		char *p = (char *)a.allocate(10*1024);
+
+		char *pointers[100];
+		for (int i=0; i<100; ++i)
+			pointers[i] = (char *)a.allocate(1024);
+		for (int i=0; i<100; ++i)
+			a.deallocate(pointers[i]);
+
+		a.deallocate(p);
+
+		for (int i=0; i<100; ++i)
+			pointers[i] = (char *)a.allocate(4*1024);
+		for (int i=0; i<100; ++i)
+			a.deallocate(pointers[i]);
+
+		memory_globals::shutdown();
+	}
 }
 
 int main(int, char **)
 {
 	test_memory();
 	test_array();
+	test_scratch();
 	return 0;
 }