Subject: Re: Nikon D70
To: Jukka Marin <jmarin@embedtronics.fi>
From: Johan Danielsson <joda@pdc.kth.se>
List: netbsd-users
Date: 07/22/2004 09:26:53
Jukka Marin <jmarin@embedtronics.fi> writes:

> Mine, too.. :-I  The dd trick didn't help.  

I've used the following with some success.

/Johan

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <unistd.h>
#include <sys/resource.h>
#include <util.h>
#include <sys/param.h>
#include <sys/sysctl.h>

int
main(int argc, char **argv)
{
    int n = 0;
    char str[10];
    unsigned char *buf, *p;

    uint64_t physmem;
    uint64_t mem_required;
    uint32_t pagesize;
    uint32_t size_needed = 65536;
    size_t osize;
    struct rlimit limit;

    osize = sizeof(physmem);
    if(sysctlbyname("hw.physmem64", &physmem, &osize, NULL, 0) < 0)
	err(1, "hw.physmem64");

    osize = sizeof(pagesize);
    if(sysctlbyname("hw.pagesize", &pagesize, &osize, NULL, 0) < 0)
	err(1, "hw.pagesize");

    mem_required = physmem / pagesize - physmem / size_needed + 1;
    mem_required *= pagesize;

    humanize_number(str, sizeof(str), physmem, "Byte", HN_AUTOSCALE, 0);
    printf("physical memory: %s\n", str);
    humanize_number(str, sizeof(str), pagesize, "Byte", HN_AUTOSCALE, 0);
    printf("page size: %s\n", str);
    humanize_number(str, sizeof(str), mem_required, "Byte", HN_AUTOSCALE, 0);
    printf("memory required: %s\n", str);

    mem_required /= 4;
    humanize_number(str, sizeof(str), mem_required, "Byte", HN_AUTOSCALE, 0);
    printf("...but trying: %s\n", str);


    if(getrlimit(RLIMIT_DATA, &limit) < 0)
	err(1, "getrlimit");
    /* XXX add ten percent to be on the safe side */
    limit.rlim_cur = mem_required + mem_required / 10;
    if(setrlimit(RLIMIT_DATA, &limit) < 0)
	err(1, "setrlimimt(RLIMIT_DATA, %lld)", limit.rlim_cur);

    buf = malloc(mem_required);
    if(buf == NULL)
	err(1, "malloc");
    for(p = buf; p < buf + mem_required; p += pagesize) {
	*p = 1;
	n++;
    }
    printf("touched %d pages\n", n);
    free(buf);
    exit(0);
}