Search code examples
iphonec++stxxl

stxxl and overriding cxx_constructor


I'm trying to use stxxl as a backing store to a vector. If I do nothing it will automatically allocate 1 gig of disk space and everything works perfectly. However I don't need that much space allocated and, in fact, I need to gracefully scale from my optimal 512 megs down to a min of 128 megs of storage.

Unfortunately I have stxxl::vector defined in my objective-c class and on instantiation of that class the cxx_constructor function is called which starts up stxxl and allocated a gig whether i like it or not.

Is there any way I can override the cxx_constructor call and add my init in before it goes on to instantiate my class? I did try and create a simple object that would get instantiated by cxx_constructor. Unfortunately, however, for some unknown reason cxx_constructor calls my class's constructor twice.

Is the only option to add a static to that class preventing it from getting instantiated more than once? This is definitely an option just not very elegant. For one I'd love to know why it gets called twice.

Any info much appreciated!

Edit: Here is the code I wrote.

namespace stxxl
{
    class Config
    {
        float GetFreeDiskspace() 
        {
            float totalSpace = 0.0f;
            float totalFreeSpace = 0.0f;
            NSError *error = nil;  
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
            NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];  

            if (dictionary) {  
                NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];  
                NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
                totalSpace = [fileSystemSizeInBytes floatValue];
                totalFreeSpace = [freeFileSystemSizeInBytes floatValue];
                NSLog(@"Memory Capacity of %f MiB with %f MiB Free memory available.", ((totalSpace/1024.0f)/1024.0f), ((totalFreeSpace/1024.0f)/1024.0f));
            } else {  
                NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);  
            }  

            return totalFreeSpace;
        }
    public:
        Config()
        {

            char cacheFileName[256];
            NSString* pTempDir      = NSTemporaryDirectory();
            strcpy( cacheFileName, [pTempDir UTF8String] );
            strcat( cacheFileName, "/stxxlcache" );

            const uint64_t  kFreeSpace          = (uint64_t)GetFreeDiskspace();

            const uint64_t kMaxCacheSize        = 512*1024*1024;
            const uint64_t kCacheSize           = (kFreeSpace > kMaxCacheSize) ? kMaxCacheSize : kFreeSpace;
            const uint64_t kMinCacheSize        = 2000 * ((1 << 15) >> 1) * sizeof( float );
            stxxl::config* pCfg  = stxxl::config::get_instance();
            pCfg->init( cacheFileName, kCacheSize, false );
        }
    };
};

I then declare it inside my app delegate as follows:

stxxl::Config                       mCallOnceConfig;

Then when I run my application with a breakpoint inside the stxxl::Config constructor the breakpoint gets hit twice. I don't declare it anywhere else. cxx_constructor calls the constructor twice.

(It also worth noting that I've added my own stxxl::config::init function and blanked out the default one)

Edit 2: Placing a static bool around the constructor's code does sort out the double intialisation and everything works as I'd expect. Its a really rubbish solution though :(


Solution

  • Ok I've come up with a fairly elegant solution to my problem. I've dropped the whole class instance method and now I define my stxxl setup within the app delegates "+ (void) initialize" function. This ONLY gets called once.

    Hopefully that will save some people some hassle if they suffer similar issues in the future :)