Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
stdlib.cpp File Reference
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include <aygshell.h>
#include "mozce_internal.h"

Go to the source code of this file.

Defines

#define _MAX_FNAME   256
#define _MAX_DIR   _MAX_FNAME
#define _MAX_EXT   _MAX_FNAME

Functions

MOZCE_SHUNT_API char * mozce_fullpath (char *absPath, const char *relPath, size_t maxLength)
MOZCE_SHUNT_API void mozce_splitpath (const char *inPath, char *outDrive, char *outDir, char *outFname, char *outExt)
MOZCE_SHUNT_API void mozce_makepath (char *outPath, const char *inDrive, const char *inDir, const char *inFname, const char *inExt)
MOZCE_SHUNT_API int mozce_strcmpi (const char *dest, const char *src)
static void setAllocationUpperLimit ()
void SendOOMPending ()
static void memoryCheck ()
static void oom ()
MOZCE_SHUNT_API voidmozce_malloc (unsigned a)
MOZCE_SHUNT_API void mozce_free (void *a)
MOZCE_SHUNT_API voidmozce_realloc (void *a, unsigned b)
MOZCE_SHUNT_API voidmozce_calloc (size_t n, size_t nelem)

Variables

static int gDelayBeforeResending = 1000
static int gFirstRun = 1
static unsigned long gTotalMemoryAllocated = 0
static unsigned long gTotalMemoryAllowed = 0
static unsigned long gLastTickCount = 0

Define Documentation

Definition at line 49 of file stdlib.cpp.

Definition at line 50 of file stdlib.cpp.

#define _MAX_FNAME   256

Definition at line 48 of file stdlib.cpp.


Function Documentation

static void memoryCheck ( ) [static]

Definition at line 289 of file stdlib.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

MOZCE_SHUNT_API void* mozce_calloc ( size_t  n,
size_t  nelem 
)

Definition at line 356 of file stdlib.cpp.

{
    gTotalMemoryAllocated += n*nelem;
    memoryCheck();

    void* buffer = calloc(n, nelem);
    if (!buffer)
    {
        SHCloseApps(n*nelem);
        buffer = calloc(n, nelem);

        if (!buffer)
            oom();
    }
    return buffer;
}

Here is the call graph for this function:

Definition at line 326 of file stdlib.cpp.

{
    if (!a)
        return;

    gTotalMemoryAllocated -= _msize(a);
    memoryCheck();

    free(a);
}

Here is the call graph for this function:

MOZCE_SHUNT_API char* mozce_fullpath ( char *  absPath,
const char *  relPath,
size_t  maxLength 
)

Definition at line 60 of file stdlib.cpp.

{
    MOZCE_PRECHECK
        
#ifdef LOG_CALLS
#ifdef DEBUG
        mozce_printf("mozce_fullpath called\n");
#endif
#endif
    
    if (relPath[0] != '\\') 
    {
        int i;
        unsigned short dir[MAX_PATH];
        GetModuleFileName(GetModuleHandle (NULL), dir, MAX_PATH);
        for (i = _tcslen(dir); i && dir[i] != TEXT('\\'); i--) {}
        
        dir[i + 1] = TCHAR('\0');
        
        w2a_buffer(dir, -1, absPath, maxLength);
    }
    strcat(absPath, relPath);
    
    return absPath;
}

Here is the call graph for this function:

MOZCE_SHUNT_API void mozce_makepath ( char *  outPath,
const char *  inDrive,
const char *  inDir,
const char *  inFname,
const char *  inExt 
)

Definition at line 197 of file stdlib.cpp.

{
    MOZCE_PRECHECK
        
#ifdef LOG_CALLS
#ifdef DEBUG
        mozce_printf("mozce_makepath called\n");
#endif
#endif
    if(NULL != outPath)
    {
        int dirLen = 0;
        if(NULL != inDir)
        {
            dirLen = strlen(inDir);
            if(dirLen)
            {
                dirLen--;
            }
        }
        _snprintf(outPath, _MAX_PATH, "%s%s%s%s%s",
                  (NULL != inDir) ? inDir : "",
                  (NULL != inDir && '\\' != inDir[dirLen] && '/' != inDir[dirLen]) ? "\\" : "",
                  (NULL != inFname) ? inFname : "",
                  (NULL != inExt && '.' != inExt[0]) ? "." : "",
                  (NULL != inExt) ? inExt : ""
                  );
    }
}

Here is the call graph for this function:

MOZCE_SHUNT_API void* mozce_malloc ( unsigned  a)

Definition at line 309 of file stdlib.cpp.

{
    gTotalMemoryAllocated += a;
    memoryCheck();

    void *buffer = malloc(a);
    if (!buffer)
    {
        SHCloseApps(a);
        buffer = malloc(a);

        if (!buffer)
            oom();
    }
    return buffer;
}

Here is the call graph for this function:

MOZCE_SHUNT_API void* mozce_realloc ( void a,
unsigned  b 
)

Definition at line 337 of file stdlib.cpp.

{
    gTotalMemoryAllocated += b;
    gTotalMemoryAllocated -= _msize(a);
    memoryCheck();
    
    void *buffer = realloc(a,b);
    if (!buffer)
    {
        SHCloseApps(b);
        buffer = realloc(a,b);

        if (!buffer)
            oom();
    }
    return buffer;
}

Here is the call graph for this function:

MOZCE_SHUNT_API void mozce_splitpath ( const char *  inPath,
char *  outDrive,
char *  outDir,
char *  outFname,
char *  outExt 
)

Definition at line 86 of file stdlib.cpp.

{
    MOZCE_PRECHECK
        
#ifdef LOG_CALLS
#ifdef DEBUG
        mozce_printf("mozce_splitpath called\n");
#endif
#endif
    if(NULL != outDrive)
    {
        *outDrive = '\0';
    }
    if(NULL != outDir)
    {
        *outDir = '\0';
    }
    if(NULL != outFname)
    {
        *outFname = '\0';
    }
    if(NULL != outExt)
    {
        *outExt = '\0';
    }
    
    if(NULL != inPath && '\0' != *inPath)
    {
        char* dup = (char*) malloc(strlen(inPath)+1);
        if(NULL != dup)
        {
            strcpy(dup, inPath);
            /*
            **  Change all forward slashes to back.
            */
            char* convert = dup;
            do
            {
                if('/' == *convert)
                {
                    *convert = '\\';
                }
                convert++;
            }
            while(*convert);
            
            /*
            **  Find last slash first.
            */
            char* slash = strrchr(dup, '\\');
            
            /*
            **  Find extension, must be after any slash.
            */
            char* ext = NULL;
            if(NULL == slash)
            {
                ext = strchr(dup, '.');
            }
            else
            {
                ext = strchr(slash, '.');
            }
            
            /*
            **  Reap extension.
            */
            if(NULL != ext)
            {
                if(NULL != outExt)
                {
                    strncpy(outExt, ext, _MAX_EXT);
                }
                
                *ext = '\0';
            }
            
            /*
            **  Reap filename.
            */
            char* fname = NULL;
            if(NULL == slash)
            {
                fname = dup;
            }
            else
            {
                fname = slash + 1;
            }
            
            if(NULL != outFname)
            {
                strncpy(outFname, fname, _MAX_FNAME);
            }
            
            *fname = '\0';
            
            /*
            **  Reap directory.
            */
            if(NULL != slash && NULL != outDir)
            {
                strncpy(outDir, dup, _MAX_DIR);
            }
            
            free(dup);
        }
    }
}

Here is the call graph for this function:

MOZCE_SHUNT_API int mozce_strcmpi ( const char *  dest,
const char *  src 
)

Definition at line 227 of file stdlib.cpp.

{
    MOZCE_PRECHECK
        
#ifdef LOG_CALLS
#ifdef DEBUG
        mozce_printf("mozce_strcmpi called\n");
#endif
#endif
    int f,l;
    
    do {
        if ( ((f = (unsigned char)(*(dest++))) >= 'A') && (f <= 'Z') )
            f -= ('A' - 'a');
        
        if ( ((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z') )
            l -= ('A' - 'a');
    } while ( f && (f == l) );
    
    return(f - l);
}

Here is the call graph for this function:

static void oom ( ) [static]

Definition at line 303 of file stdlib.cpp.

{
    // We can't really do anything else.
    SendOOMPending();
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 269 of file stdlib.cpp.

{

    DWORD lastTickCount = GetTickCount();

    if (lastTickCount >= gLastTickCount + gDelayBeforeResending)
    {
        char buffer[4] = "OOM";
        COPYDATASTRUCT cds = { 0, 4, buffer };
        
        // XXX The name of this window is Minimo specific.
        // Probably should generalize.
        
        HWND a = FindWindowW(L"MINIMO_LISTENER", NULL);
        PostMessage(a, WM_COPYDATA, NULL, (LPARAM)&cds); 

        gLastTickCount = GetTickCount();
    }
}

Here is the caller graph for this function:

static void setAllocationUpperLimit ( ) [static]

Definition at line 257 of file stdlib.cpp.

{
    MEMORYSTATUS mst;
    mst.dwLength  = sizeof(MEMORYSTATUS);
    GlobalMemoryStatus(&mst);

    gTotalMemoryAllowed = mst.dwAvailPhys / 2; /* XXX should be configurable */

    gLastTickCount = GetTickCount() - gDelayBeforeResending;
}

Here is the caller graph for this function:


Variable Documentation

int gDelayBeforeResending = 1000 [static]

Definition at line 251 of file stdlib.cpp.

int gFirstRun = 1 [static]

Definition at line 252 of file stdlib.cpp.

unsigned long gLastTickCount = 0 [static]

Definition at line 255 of file stdlib.cpp.

unsigned long gTotalMemoryAllocated = 0 [static]

Definition at line 253 of file stdlib.cpp.

unsigned long gTotalMemoryAllowed = 0 [static]

Definition at line 254 of file stdlib.cpp.