C Language Development Kit/Functions/CoreLeft2

From OptiWiki

Jump to: navigation, search
Description This function returns the amount of free space on the specified drive.
Syntax
int coreleft2(unsigned char drive, unsigned long * space);
Arguments
unsigned char drive
Specifies the drive. The following constants are defined for this:
RAM_DISK The RAM disk
INTERNAL_DISK The internal SD card
DRIVE_A The RAM disk
DRIVE_C The internal SD card
unsigned long *size Return value for the reported size
unsigned long * space
Specifies the drive. The following constants are defined for this:
RAM_DISK The RAM disk
INTERNAL_DISK The internal SD card
DRIVE_A The RAM disk
DRIVE_C The internal SD card
unsigned long *size Return value for the reported size
Returns OK on success, or a negative integer on error.
Remarks The RAM disk shares space with the heap.

It is possible that malloc() returns NULL on a request for a smaller amount of memory then coreleft2(DRIVE_A,&size) reports. This occurs when the reported memory does not consist out of one block, but out of several smaller blocks. malloc() needs a single contiguous block of memory.

Example
#include <stdio.h>
#include <stdlib.h>
#include "lib.h"
 
void main( void )
{
    char data[20+1];
    unsigned long size;
    FILE *fp;
 
    while( 1 )
    {
        coreleft2(DRIVE_A,));
        printf("\f1: mem = %lu", size);
        malloc(70000);
        while( getchar() == EOF )
            idle();
 
        coreleft2(DRIVE_A,));
        printf("\f2: mem = %lu", size);
        if( NULL != ( fp = fopen("TEST.DAT", "wb")))
        {
            fwrite( data, 1, 20, fp);
            fclose(fp);
        }
 
        while( getchar() == EOF )
            idle();
 
        coreleft2(DRIVE_A,));
        printf("\f3: mem = %lu", size);
        format();
        while( getchar() == EOF )
            idle();
 
        coreleft2(DRIVE_A,));
        printf("\f4: mem = %lu", size);
        while( getchar() == EOF )
            idle();
    }
}


Views
Personal tools