Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "io_x" <a@b.c.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| References | <70cf2865-5c4a-47a5-a8e8-ac369c2b91b3@k22g2000yqh.googlegroups.com> |
| Subject | Re: Would someone tell me if I'm on the wrong track? |
| Date | 2011-05-11 08:31 +0200 |
| Message-ID | <4dca2cf0$0$18239$4fafbaef@reader2.news.tin.it> (permalink) |
| Organization | TIN.IT (http://www.tin.it) |
"luser- -droog" <mijoryx@yahoo.com> ha scritto nel messaggio
news:70cf2865-5c4a-47a5-a8e8-ac369c2b91b3@k22g2000yqh.googlegroups.com...
> I'm trying to build a new memory allocation scheme for
> my postscript interpreter to satisfy these requirements:
> % pointer + offset + size + short tag <= 32bits
> % 'garbage-collection'-ready (structure the heap for easy iteration)
> % all memory is in one region for easy restore from disk
>
> So I've written a prototype where the "pointer" is an index
> into an extendable address table stored in the heap itself.
> And it seems to work! But I don't trust it, you know?
>
> I'd appreciate any comments the experts here could offer.
>
> One apology. I'm trying to use mmap if available or malloc
> as a fallback, but ifdefs, it seems, can't be made pretty.
> Or, at least I have yet to discover how, unless it's just to
> "put it in a file you don't need to look at". :)
> 569(1)01:27 AM:proto 0> make v
> cc -g -Wall v.c -o v
> 570(1)01:27 AM:proto 0> v
> put seven, got a 7
> 571(1)01:27 AM:proto 0> cat v.c
> #define MMAP
> #define _GNU_SOURCE
>
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> #include <unistd.h>
>
> #ifdef MMAP
> #include <sys/mman.h>
> #endif /* otherwise, use malloc/realloc/free */
>
> void error(char *msg) {
> fprintf(stderr, "%s\n", msg);
> exit(EXIT_FAILURE);
> }
>
> unsigned pgsz /*= getpagesize()*/;
>
> typedef struct {
> unsigned char *base;
> unsigned used;
> unsigned max;
> } mfile;
>
> /* initialize the memory file */
> void initmem(mfile *mem) {
> #ifdef MMAP
> mem->base = mmap(NULL, pgsz, PROT_READ|PROT_WRITE, MAP_SHARED
> |MAP_ANONYMOUS,-1,0 );
> if (mem->base == MAP_FAILED)
> #else
> mem->base = malloc(pgsz);
> if (mem->base == NULL)
> #endif
> error("unable to initialize memory file");
> mem->used = 0;
> mem->max = pgsz;
> }
>
> /* destroy memory file */
> void exitmem(mfile *mem) {
> #ifdef MMAP
> munmap(mem->base, mem->max);
> #else
> free(mem->base);
> #endif
> mem->base = NULL;
> mem->used = 0;
> mem->max = 0;
> }
>
> /* grow memory by sz */
> void growmem(mfile *mem, unsigned sz) {
> unsigned char *tmp;
> if (sz < pgsz) sz = pgsz;
> else sz = (sz/pgsz + 1) * pgsz;
what about if this mult above overflow?
> sz += mem->max;
what about if this sz += overflow?
i not understand well the remain (too much complex
for my little think)
but i think, it is better to see if sz overflow
and in input to write "if((int) sz <0) return -1;"
so that each function can fail.
never exist one function can not fail
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Would someone tell me if I'm on the wrong track? luser- -droog <mijoryx@yahoo.com> - 2011-05-09 23:47 -0700
Re: Would someone tell me if I'm on the wrong track? luser- -droog <mijoryx@yahoo.com> - 2011-05-10 00:05 -0700
Re: Would someone tell me if I'm on the wrong track? "io_x" <a@b.c.invalid> - 2011-05-11 08:31 +0200
Re: Would someone tell me if I'm on the wrong track? luser- -droog <mijoryx@yahoo.com> - 2011-05-10 23:46 -0700
Re: Would someone tell me if I'm on the wrong track? luser- -droog <mijoryx@yahoo.com> - 2011-05-11 23:05 -0700
Re: Would someone tell me if I'm on the wrong track? Seebs <usenet-nospam@seebs.net> - 2011-05-12 07:36 +0000
Re: Would someone tell me if I'm on the wrong track? pete <pfiland@mindspring.com> - 2011-05-12 07:51 -0400
Re: Would someone tell me if I'm on the wrong track? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-14 03:34 +0100
Re: Would someone tell me if I'm on the wrong track? pete <pfiland@mindspring.com> - 2011-05-13 23:19 -0400
Re: Would someone tell me if I'm on the wrong track? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-14 12:02 +0100
Re: Would someone tell me if I'm on the wrong track? pete <pfiland@mindspring.com> - 2011-05-12 08:01 -0400
Re: Would someone tell me if I'm on the wrong track? Seebs <usenet-nospam@seebs.net> - 2011-05-12 15:57 +0000
Re: Would someone tell me if I'm on the wrong track? pete <pfiland@mindspring.com> - 2011-05-12 18:04 -0400
Re: Would someone tell me if I'm on the wrong track? Seebs <usenet-nospam@seebs.net> - 2011-05-12 22:18 +0000
Re: Would someone tell me if I'm on the wrong track? luser- -droog <mijoryx@yahoo.com> - 2011-05-12 23:05 -0700
Re: Would someone tell me if I'm on the wrong track? "io_x" <a@b.c.invalid> - 2011-05-13 10:21 +0200
Re: Would someone tell me if I'm on the wrong track? luser- -droog <mijoryx@yahoo.com> - 2011-05-12 09:33 -0700
Re: Would someone tell me if I'm on the wrong track? Peter Nilsson <airia@acay.com.au> - 2011-05-12 22:47 -0700
Re: Would someone tell me if I'm on the wrong track? "io_x" <a@b.c.invalid> - 2011-05-13 10:21 +0200
Re: Would someone tell me if I'm on the wrong track? Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-18 22:21 -0700
Re: Would someone tell me if I'm on the wrong track? Peter Nilsson <airia@acay.com.au> - 2011-05-18 23:47 -0700
Re: Would someone tell me if I'm on the wrong track? Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-19 11:00 -0700
csiph-web