Change initialize a bit
[c11tester.git] / mymemory.cc
index 1dada053c2ba9060ed1c3301e3b895c44bf3855c..dd22909c9bb31a32405070cb2e6873ac5d584e95 100644 (file)
-#include "mymemory.h"
-#include "snapshot.h"
-#include "snapshotimp.h"
+
+#include <stdlib.h>
 #include <stdio.h>
 #include <dlfcn.h>
-#if !USE_MPROTECT_SNAPSHOT
-static mspace sStaticSpace = NULL;
-#endif
-
-void *MYMALLOC(size_t size) {
-#if USE_MPROTECT_SNAPSHOT
-       static void *(*mallocp)(size_t size);
-       char *error;
-       void *ptr;
-  
-       /* get address of libc malloc */
-       if (!mallocp) {
-               mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
-               if ((error = dlerror()) != NULL) {
-                       fputs(error, stderr);
-                       exit(EXIT_FAILURE);
-               }
-       }
-       ptr = mallocp(size);     
-       return ptr;
-#else
-       if( !sTheRecord ){
-               createSharedLibrary();
-       }
-       if( NULL == sStaticSpace )
-               sStaticSpace = create_mspace_with_base( ( void * )( sTheRecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct Snapshot_t ), 1 );
-       return mspace_malloc( sStaticSpace, size );
-#endif
+#include <unistd.h>
+#include <string.h>
+#include <new>
+
+#include "mymemory.h"
+#include "snapshot.h"
+#include "common.h"
+#include "threads-model.h"
+#include "model.h"
+#include "datarace.h"
+
+#define REQUESTS_BEFORE_ALLOC 1024
+
+size_t allocatedReqs[REQUESTS_BEFORE_ALLOC] = { 0 };
+int nextRequest = 0;
+int howManyFreed = 0;
+mspace sStaticSpace = NULL;
+
+/** Non-snapshotting calloc for our use. */
+void *model_calloc(size_t count, size_t size)
+{
+       return mspace_calloc(sStaticSpace, count, size);
 }
 
-void *system_malloc( size_t size ){
-       static void *(*mallocp)(size_t size);
-       char *error;
-       void *ptr;
-
-       /* get address of libc malloc */
-       if (!mallocp) {
-               mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
-               if ((error = dlerror()) != NULL) {
-                       fputs(error, stderr);
-                       exit(EXIT_FAILURE);
-               }
-       }
-       ptr = mallocp(size);
-       return ptr;
+/** Non-snapshotting malloc for our use. */
+void *model_malloc(size_t size)
+{
+       return mspace_malloc(sStaticSpace, size);
 }
 
-void system_free( void * ptr ){
-       static void (*freep)(void *);
-       char *error;
-
-       /* get address of libc free */
-       if (!freep) {
-               freep = ( void  ( * )( void * ) )dlsym(RTLD_NEXT, "free");
-               if ((error = dlerror()) != NULL) {
-                       fputs(error, stderr);
-                       exit(EXIT_FAILURE);
-               }
-       }
-       freep(ptr);
+/** Non-snapshotting malloc for our use. */
+void *model_realloc(void *ptr, size_t size)
+{
+       return mspace_realloc(sStaticSpace, ptr, size);
 }
-void MYFREE(void *ptr) {
-#if USE_MPROTECT_SNAPSHOT
-       static void (*freep)(void *);
-       char *error;
-
-       /* get address of libc free */
-       if (!freep) {
-               freep = ( void  ( * )( void * ) )dlsym(RTLD_NEXT, "free");
-               if ((error = dlerror()) != NULL) {
-                       fputs(error, stderr);
-                       exit(EXIT_FAILURE);
-               }
-       }
-       freep(ptr);
-#else
-       mspace_free( sStaticSpace, ptr );
-#endif
+
+/** @brief Snapshotting malloc, for use by model-checker (not user progs) */
+void * snapshot_malloc(size_t size)
+{
+       void *tmp = mspace_malloc(model_snapshot_space, size);
+       ASSERT(tmp);
+       return tmp;
 }
-mspace mySpace = NULL;
-void * basemySpace = NULL;
 
-void *malloc( size_t size ) {
-       return mspace_malloc( mySpace, size );
+/** @brief Snapshotting calloc, for use by model-checker (not user progs) */
+void * snapshot_calloc(size_t count, size_t size)
+{
+       void *tmp = mspace_calloc(model_snapshot_space, count, size);
+       ASSERT(tmp);
+       return tmp;
 }
 
-void free( void * ptr ){
-       mspace_free( mySpace, ptr );
+/** @brief Snapshotting realloc, for use by model-checker (not user progs) */
+void *snapshot_realloc(void *ptr, size_t size)
+{
+       void *tmp = mspace_realloc(model_snapshot_space, ptr, size);
+       ASSERT(tmp);
+       return tmp;
 }
 
-void *realloc( void *ptr, size_t size ){
-       return mspace_realloc( mySpace, ptr, size );
+/** @brief Snapshotting free, for use by model-checker (not user progs) */
+void snapshot_free(void *ptr)
+{
+       mspace_free(model_snapshot_space, ptr);
 }
 
-void * operator new(size_t size) throw(std::bad_alloc) {
-       return malloc(size);
+/** Non-snapshotting free for our use. */
+void model_free(void *ptr)
+{
+       mspace_free(sStaticSpace, ptr);
 }
 
-void operator delete(void *p) throw() {
-       free(p);
+/** Bootstrap allocation. Problem is that the dynamic linker calls require
+ *  calloc to work and calloc requires the dynamic linker to work. */
+
+#define BOOTSTRAPBYTES 131072
+char bootstrapmemory[BOOTSTRAPBYTES];
+size_t offset = 0;
+
+void * HandleEarlyAllocationRequest(size_t sz)
+{
+       /* Align to 8 byte boundary */
+       sz = (sz + 7) & ~7;
+
+       if (sz > (BOOTSTRAPBYTES-offset)) {
+               model_print("OUT OF BOOTSTRAP MEMORY.  Increase the size of BOOTSTRAPBYTES in mymemory.cc\n");
+               exit(EXIT_FAILURE);
+       }
+
+       void *pointer = (void *)&bootstrapmemory[offset];
+       offset += sz;
+       return pointer;
 }
 
-void * operator new[](size_t size) throw(std::bad_alloc) {
-       return malloc(size);
+/** @brief Global mspace reference for the model-checker's snapshotting heap */
+mspace model_snapshot_space = NULL;
+
+/** @brief Snapshotting allocation function for use by the Thread class only */
+void * Thread_malloc(size_t size)
+{
+       return snapshot_malloc(size);
 }
 
-void operator delete[](void *p, size_t size) {
-       free(p);
+/** @brief Snapshotting free function for use by the Thread class only */
+void Thread_free(void *ptr)
+{
+       snapshot_free(ptr);
 }