Commits

Anonymous committed cf21865

Added memory::pointer_add/pointer_sub
Build with mingw on windows.

Comments (0)

Files changed (4)

 	}
 
 	template <typename T>
-	inline Array<T>::Array(Allocator &allocator) : _allocator(&allocator), _size(0), _capacity(0), _data(NULL) {}
+	inline Array<T>::Array(Allocator &allocator) : _allocator(&allocator), _size(0), _capacity(0), _data(0) {}
 
 	template <typename T>
 	inline Array<T>::~Array()
 #include "memory.h"
 
+#include <stdlib.h>
 #include <assert.h>
 #include <new>
 
 
 	namespace memory {
 		inline void *align_forward(void *p, uint32_t align);
+		inline void *pointer_add(void *p, uint32_t bytes);
+		inline const void *pointer_add(const void *p, uint32_t bytes);
+		inline void *pointer_sub(void *p, uint32_t bytes);
+		inline const void *pointer_sub(const void *p, uint32_t bytes);
 	}
 
 	// ---------------------------------------------------------------
 			pi += (align - mod);
 		return (void *)pi;
 	}
+
+	/// Returns the result of advancing p by the specified number of bytes
+	inline void *memory::pointer_add(void *p, uint32_t bytes)	{
+		return (void*)((char *)p + bytes);
+	}
+
+	inline const void *memory::pointer_add(const void *p, uint32_t bytes)	{
+		return (const void*)((const char *)p + bytes);
+	}
+
+	/// Returns the result of moving p back by the specified number of bytes
+	inline void *memory::pointer_sub(void *p, uint32_t bytes)	{
+		return (void*)((char *)p - bytes);
+	}
+
+	inline const void *memory::pointer_sub(const void *p, uint32_t bytes)	{
+		return (const void*)((const char *)p - bytes);
+	}
 }
 #include "memory.h"
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <assert.h>
 
 #define ASSERT(x) assert(x)
 		uint64_t h = murmur_hash_64(s, strlen(s), 0);
 		ASSERT(h == 0xe604acc23b568f83ull);
 	}
+
+	void test_pointer_arithmetic()
+	{
+		const char check = 0xfe;
+		const unsigned test_size = 128;
+
+		TempAllocator512 ta;
+		Array<char> buffer(ta);
+		array::set_capacity(buffer, test_size);
+		memset(array::begin(buffer), 0, array::size(buffer));
+
+		void* data = array::begin(buffer);
+		for (unsigned i = 0; i != test_size; ++i) {
+			buffer[i] = check;
+			char* value = (char*)memory::pointer_add(data, i);
+			ASSERT(*value == buffer[i]);
+		}
+	}
 }
 
 int main(int, char **)
 	test_temp_allocator();
 	test_hash();
 	test_murmur_hash();
+	test_pointer_arithmetic();
 	return 0;
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.