Hendrik Lipka / ntp_mem

Description: program to test a possible memory leak when using NTP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AvailableMemory.cpp Source File

AvailableMemory.cpp

#include "AvailableMemory.h"
#include <stdlib.h>
#include <stdio.h>

00005 namespace segundo {
00006 namespace Utilities {

00008 int AvailableMemory(int resolution, int maximum, bool disableInterrupts) {

    if (resolution < 1) resolution = 1;
    if (maximum < 0) maximum = 0;

    int low = 0;
    int high = maximum + 1;

    if (disableInterrupts) __disable_irq();

    while (high - low > resolution) {
        int mid = (low + high) / 2;
        printf("try malloc %i bytes\n",mid);
        void* p = malloc(mid);
        if (p == NULL) {
            high = mid;
        } else {
            free(p);
            low = mid;
        }
    }

    if (disableInterrupts) __enable_irq();

    return low;
}

} // namespace Utilities
} // namespace segundo