Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3666 > unrolled thread

Would someone tell me if I'm on the wrong track?

Started byluser- -droog <mijoryx@yahoo.com>
First post2011-05-09 23:47 -0700
Last post2011-05-19 11:00 -0700
Articles 20 on this page of 22 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  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

Page 1 of 2  [1] 2  Next page →


#3666 — Would someone tell me if I'm on the wrong track?

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-09 23:47 -0700
SubjectWould someone tell me if I'm on the wrong track?
Message-ID<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;
    sz += mem->max;
#ifdef MMAP
    tmp = mremap(mem->base, mem->max, sz, MREMAP_MAYMOVE);
    if (tmp == MAP_FAILED)
#else
    tmp = realloc(mem->base, sz);
    if (!tmp)
#endif
        error("unable to grow memory");
    mem->base = tmp;
    mem->max = sz;
}

/* allocate memory, returns offset in memory file */
unsigned mfalloc(mfile *mem, unsigned sz) {
    unsigned adr = mem->used;
    if (sz + mem->used > mem->max) growmem(mem,sz);
    mem->used += sz;
    return adr;
}


#define TABSZ 1000
typedef struct {
    unsigned nexttab;
    unsigned nextent;
    struct {
        unsigned adr;
    } tab[TABSZ];
} mtab;

/* allocate and initialize a new table */
unsigned initmtab(mfile *mem) {
    unsigned adr;
    adr = mfalloc(mem, sizeof(mtab)); /*create mtab at address 0*/
    memset(mem->base + adr, 0, sizeof(mtab));
    return adr;
}

/* allocate memory, returns table index */
unsigned mtalloc(mfile *mem, unsigned mtabloc, unsigned sz) {
    mtab *tab = (void *)(mem->base + mtabloc);
    if (tab->nextent >= TABSZ)
        return mtalloc(mem, tab->nexttab, sz);
    else {
        unsigned ent = tab->nextent;
        tab->nextent++;
        tab->tab[ent].adr = mfalloc(mem, sz);
        if (tab->nextent == TABSZ) {
            tab->nexttab = initmtab(mem);
        }
        return ent;
    }
}

/* fetch a value from a composite object */
void get(mfile *mem, unsigned ent, unsigned offset, unsigned sz, void
*dest) {
    mtab *tab;
    unsigned mtabloc = 0;
    tab = (void *)(mem->base + mtabloc);
    while (ent >= TABSZ) {
        tab = (void *)(mem->base + mtabloc);
        mtabloc = tab->nexttab;
        ent -= TABSZ;
    }
    memcpy(dest, mem->base + tab->tab[ent].adr + offset*sz, sz);
}

/* put a value into a composite object */
void put(mfile *mem, unsigned ent, unsigned offset, unsigned sz, void
*src) {
    mtab *tab;
    unsigned mtabloc = 0;
    tab = (void *)(mem->base + mtabloc);
    while (ent >= TABSZ) {
        tab = (void *)(mem->base + mtabloc);
        mtabloc = tab->nexttab;
        ent -= TABSZ;
    }
    memcpy(mem->base + tab->tab[ent].adr + offset*sz, src, sz);
}

mfile mem;

/* initialize everything */
void init(void) {
    pgsz = getpagesize();
    initmem(&mem);
    (void)initmtab(&mem); /* init table zero */
}

/* destroy everything */
void xit(void) {
    exitmem(&mem);
}


int main() {
    init();
    unsigned adr;
    int seven = 7;
    int ret;
    adr = mtalloc(&mem, 0, sizeof seven);
    put(&mem, adr, 0, sizeof seven, &seven);
    get(&mem, adr, 0, sizeof seven, &ret);
    printf("put seven, got a %d\n", ret);

    xit();
    return 0;
}

--
572(1)01:27 AM:proto 0> !ni
nice -n 19 firefox &
[3] 4978
573(2)01:27 AM:proto 0>

[toc] | [next] | [standalone]


#3667

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-10 00:05 -0700
Message-ID<b83f8ae9-8079-4498-9f30-bcfd94778530@s9g2000yqm.googlegroups.com>
In reply to#3666
On May 10, 1:47 am, luser- -droog <mijo...@yahoo.com> wrote:
> 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?
[...]

A little more testing always helps.
It still looks good. I feel a little better.

581(2)02:02 AM:proto 0> tail -20 v.c
    printf("put seven, got a %d\n", ret);

    unsigned adr2;
    adr2 = mtalloc(&mem, 0, 8*sizeof seven);
    put(&mem, adr2, 6, sizeof seven, &seven);
    get(&mem, adr2, 6, sizeof seven, &ret);
    printf("put seven in slot 7, got a %d\n", ret);

    unsigned adr3;
    char str[] = "beads in buddha's necklace";
    char sret[sizeof str];
    adr3 = mtalloc(&mem, 0, strlen(str)+1);
    put(&mem, adr3, 0, sizeof str, str);
    get(&mem, adr3, 0, sizeof str, sret);
    printf("stored and retrieved %s\n", sret);

    xit();
    return 0;
}

582(2)02:02 AM:proto 0> v
put seven, got a 7
put seven in slot 7, got a 7
stored and retrieved beads in buddha's necklace
583(2)02:02 AM:proto 0>

[toc] | [prev] | [next] | [standalone]


#3724

From"io_x" <a@b.c.invalid>
Date2011-05-11 08:31 +0200
Message-ID<4dca2cf0$0$18239$4fafbaef@reader2.news.tin.it>
In reply to#3666
"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



[toc] | [prev] | [next] | [standalone]


#3726

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-10 23:46 -0700
Message-ID<7add2245-16a1-495d-bff7-9bdefcd1947a@s2g2000yql.googlegroups.com>
In reply to#3724
On May 11, 1:31 am, "io_x" <a...@b.c.invalid> wrote:
> "luser- -droog" <mijo...@yahoo.com> ha scritto nel messaggionews: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?

That shouldn't be a problem for my application as sizes
will be limited to 16bit quantities.

> >    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

Yeah, you're right. There should be a lot more checking
throughout.

[toc] | [prev] | [next] | [standalone]


#3788

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-11 23:05 -0700
Message-ID<35d5a07f-b868-4f6e-83fd-e333baba79fc@gu8g2000vbb.googlegroups.com>
In reply to#3724
On May 11, 1:31 am, "io_x" <a...@b.c.invalid> wrote:
> "luser- -droog" <mijo...@yahoo.com> ha scritto nel messaggionews: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.
>
[...]
> > /* 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

unsigned sz;
(int) sz < 0

Does this just check the high bit (assuming 2's complement)?

[toc] | [prev] | [next] | [standalone]


#3789

FromSeebs <usenet-nospam@seebs.net>
Date2011-05-12 07:36 +0000
Message-ID<slrnisn3ei.108u.usenet-nospam@guild.seebs.net>
In reply to#3788
On 2011-05-12, luser- -droog <mijoryx@yahoo.com> wrote:
> unsigned sz;
> (int) sz < 0
>
> Does this just check the high bit (assuming 2's complement)?

Not necessarily.

If you're comfortable assuming 2s complement, I bet you can do better with:
	sz > INT_MAX

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#3796

Frompete <pfiland@mindspring.com>
Date2011-05-12 07:51 -0400
Message-ID<4DCBC9AE.6BA@mindspring.com>
In reply to#3789
Seebs wrote:
> 
> On 2011-05-12, luser- -droog <mijoryx@yahoo.com> wrote:
> > unsigned sz;
> > (int) sz < 0
> >
> > Does this just check the high bit (assuming 2's complement)?
> 
> Not necessarily.
> 
> If you're comfortable assuming 2s complement,
> I bet you can do better with:
>         sz > INT_MAX

I don't see anything unusual 
about an unsigned int object
holding a value greater than INT_MAX.

-- 
pete

[toc] | [prev] | [next] | [standalone]


#3929

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2011-05-14 03:34 +0100
Message-ID<0.3cd58643cabd7ca9ddca.20110514033459BST.87wrhuxb58.fsf@bsb.me.uk>
In reply to#3789
Seebs <usenet-nospam@seebs.net> writes:

> On 2011-05-12, luser- -droog <mijoryx@yahoo.com> wrote:
>> unsigned sz;
>> (int) sz < 0
>>
>> Does this just check the high bit (assuming 2's complement)?
>
> Not necessarily.
>
> If you're comfortable assuming 2s complement, I bet you can do better with:
> 	sz > INT_MAX

The assumption is not 2's complement as far as I can see.  Your test
works in all of C's three permitted signed integer representations (how
can it not? -- it does not involve any signed integers).  It fails if
unsigned int has more then one more value bit that signed int.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#3930

Frompete <pfiland@mindspring.com>
Date2011-05-13 23:19 -0400
Message-ID<4DCDF4CC.7B20@mindspring.com>
In reply to#3929
Ben Bacarisse wrote:
> 
> Seebs <usenet-nospam@seebs.net> writes:
> 
> > On 2011-05-12, luser- -droog <mijoryx@yahoo.com> wrote:
> >> unsigned sz;
> >> (int) sz < 0
> >>
> >> Does this just check the high bit (assuming 2's complement)?
> >
> > Not necessarily.
> >
> > If you're comfortable assuming 2s complement, I bet you can do better with:
> >       sz > INT_MAX
> 
> The assumption is not 2's complement as far as I can see.  Your test
> works in all of C's three permitted signed integer representations (how
> can it not? -- it does not involve any signed integers).  It fails if
> unsigned int has more then one more value bit that signed int.

Also if
unsigned int has less than one more value bit than signed int.

-- 
pete

[toc] | [prev] | [next] | [standalone]


#3940

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2011-05-14 12:02 +0100
Message-ID<0.9996073548972c6c701b.20110514120250BST.87iptdy279.fsf@bsb.me.uk>
In reply to#3930
pete <pfiland@mindspring.com> writes:

> Ben Bacarisse wrote:
>> 
>> Seebs <usenet-nospam@seebs.net> writes:
>> 
>> > On 2011-05-12, luser- -droog <mijoryx@yahoo.com> wrote:
>> >> unsigned sz;
>> >> (int) sz < 0
>> >>
>> >> Does this just check the high bit (assuming 2's complement)?
>> >
>> > Not necessarily.
>> >
>> > If you're comfortable assuming 2s complement, I bet you can do better with:
>> >       sz > INT_MAX
>> 
>> The assumption is not 2's complement as far as I can see.  Your test
>> works in all of C's three permitted signed integer representations (how
>> can it not? -- it does not involve any signed integers).  It fails if
>> unsigned int has more then one more value bit that signed int.
>
> Also if
> unsigned int has less than one more value bit than signed int.

Yes.  It would have been simpler just to say that it fails unless
unsigned int has exactly one more value bit than signed int.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#3798

Frompete <pfiland@mindspring.com>
Date2011-05-12 08:01 -0400
Message-ID<4DCBCC1F.41A5@mindspring.com>
In reply to#3788
luser- -droog wrote:
> 
> On May 11, 1:31 am, "io_x" <a...@b.c.invalid> wrote:
> > "luser- -droog" <mijo...@yahoo.com> ha scritto nel messaggionews: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.
> >
> [...]
> > > /* 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
> 
> unsigned sz;
> (int) sz < 0
> 
> Does this just check the high bit (assuming 2's complement)?

I don't know, 
but I don't see the relevance of ((int) sz < 0).

If you want to check for wrap around after (sz += mem->max), then 

    if (sz - mem->max > sz) ThenItWrapped;

-- 
pete

[toc] | [prev] | [next] | [standalone]


#3812

FromSeebs <usenet-nospam@seebs.net>
Date2011-05-12 15:57 +0000
Message-ID<slrniso0q3.1his.usenet-nospam@guild.seebs.net>
In reply to#3798
On 2011-05-12, pete <pfiland@mindspring.com> wrote:
>> unsigned sz;
>> (int) sz < 0

>> Does this just check the high bit (assuming 2's complement)?

> I don't know, 
> but I don't see the relevance of ((int) sz < 0).

The relevance was in that question there.

On a typical 2s complement system, it is quite likely that if you convert
a value greater than INT_MAX but no larger than UINT_MAX to a signed integer
type, you will end up with a negative value.  Thus,
	sz > INT_MAX
and
	(int) sz < 0
are both fancy ways of saying something much like:

	(sz & ((UNIT_MAX) & ~(UINT_MAX >> 1))) != 0

(unless my lack of morning coffee made me get that wrong...)

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#3842

Frompete <pfiland@mindspring.com>
Date2011-05-12 18:04 -0400
Message-ID<4DCC597D.648C@mindspring.com>
In reply to#3812
Seebs wrote:
> 
> On 2011-05-12, pete <pfiland@mindspring.com> wrote:
> >> unsigned sz;
> >> (int) sz < 0
> 
> >> Does this just check the high bit (assuming 2's complement)?
> 
> > I don't know,
> > but I don't see the relevance of ((int) sz < 0).
> 
> The relevance was in that question there.
> 
> On a typical 2s complement system,
> it is quite likely that if you convert
> a value greater than INT_MAX but no larger than UINT_MAX
> to a signed integer type,
> you will end up with a negative value.  Thus,
>         sz > INT_MAX
> and
>         (int) sz < 0
> are both fancy ways of saying something much like:
> 
>         (sz & ((UNIT_MAX) & ~(UINT_MAX >> 1))) != 0
> 
> (unless my lack of morning coffee made me get that wrong...)

But 
why would you care about
whether or not an unsigned object
held a value which was greater than INT_MAX?

-- 
pete

[toc] | [prev] | [next] | [standalone]


#3845

FromSeebs <usenet-nospam@seebs.net>
Date2011-05-12 22:18 +0000
Message-ID<slrnisomjg.201i.usenet-nospam@guild.seebs.net>
In reply to#3842
On 2011-05-12, pete <pfiland@mindspring.com> wrote:
> But 
> why would you care about
> whether or not an unsigned object
> held a value which was greater than INT_MAX?

Reasons might include:
* Want to check whether high bit is set
* Want to be sure it's well-defined to convert it to int

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#3890

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-12 23:05 -0700
Message-ID<a4c3ad37-8227-4a79-86f4-01aab213ae8b@n11g2000yqf.googlegroups.com>
In reply to#3842
On May 12, 5:04 pm, pete <pfil...@mindspring.com> wrote:
> Seebs wrote:
>
> > On 2011-05-12, pete <pfil...@mindspring.com> wrote:
> > >> unsigned sz;
> > >> (int) sz < 0
>
> > >> Does this just check the high bit (assuming 2's complement)?
>
> > > I don't know,
> > > but I don't see the relevance of ((int) sz < 0).
>
> > The relevance was in that question there.
>
> > On a typical 2s complement system,
> > it is quite likely that if you convert
> > a value greater than INT_MAX but no larger than UINT_MAX
> > to a signed integer type,
> > you will end up with a negative value.  Thus,
> >         sz > INT_MAX
> > and
> >         (int) sz < 0
> > are both fancy ways of saying something much like:
>
> >         (sz & ((UNIT_MAX) & ~(UINT_MAX >> 1))) != 0
>
> > (unless my lack of morning coffee made me get that wrong...)
>
> But
> why would you care about
> whether or not an unsigned object
> held a value which was greater than INT_MAX?
>
> --
> pete

Well, I don't, really. But the only response my code got
was a cryptic message from io_x containing this bit
of c as an example. So it caught my eye and I wondered:
what the heck is this trying to do?

I think the gist of what io_x was trying to say is:
don't trust your input values in a function;
check that they are all in a meaningful range.

I think he recommended this code as a quick-and-dirty
failsafe check for the validity of the sz parameter.
Hitting the high-bit would mean attempting to allocate
more than 1 gigabyte in my delicate little suite of
functions.

But he may have picked that parameter arbitrarily.
I'd be much more worried about overflowing the mem->max
or mem->used values.

[toc] | [prev] | [next] | [standalone]


#3893

From"io_x" <a@b.c.invalid>
Date2011-05-13 10:21 +0200
Message-ID<4dcce9d2$0$38646$4fafbaef@reader1.news.tin.it>
In reply to#3890
"luser- -droog" <mijoryx@yahoo.com> ha scritto nel messaggio
news:a4c3ad37-8227-4a79-86f4-01aab213ae8b@n11g2000yqf.googlegroups.com...
On May 12, 5:04 pm, pete <pfil...@mindspring.com> wrote:
> Seebs wrote:
>
> > On 2011-05-12, pete <pfil...@mindspring.com> wrote:
> > >> unsigned sz;
> > >> (int) sz < 0
>
> > >> Does this just check the high bit (assuming 2's complement)?
>
> > > I don't know,
> > > but I don't see the relevance of ((int) sz < 0).
>
> > The relevance was in that question there.
>
> > On a typical 2s complement system,
> > it is quite likely that if you convert
> > a value greater than INT_MAX but no larger than UINT_MAX
> > to a signed integer type,
> > you will end up with a negative value. Thus,
> > sz > INT_MAX
> > and
> > (int) sz < 0
> > are both fancy ways of saying something much like:
>
> > (sz & ((UNIT_MAX) & ~(UINT_MAX >> 1))) != 0
>
> > (unless my lack of morning coffee made me get that wrong...)
>
> But
> why would you care about
> whether or not an unsigned object
> held a value which was greater than INT_MAX?
>
> --
> pete

Well, I don't, really. But the only response my code got
was a cryptic message from io_x containing this bit
of c as an example. So it caught my eye and I wondered:
what the heck is this trying to do?

I think the gist of what io_x was trying to say is:
don't trust your input values in a function;
check that they are all in a meaningful range.

....
----------
ok i mean above; but it is difficult to calculate the right
range, so because i'm a little lazy
 so i use one standard way for index "parameter"<Unsigned_max/2
here == (int)"parameter" but  not always;
the day i need all the unsigned as index i change



[toc] | [prev] | [next] | [standalone]


#3814

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-12 09:33 -0700
Message-ID<b42517a4-45ed-4031-9014-f6b3aae05e4a@k3g2000prl.googlegroups.com>
In reply to#3798
On May 12, 7:01 am, pete <pfil...@mindspring.com> wrote:
> luser- -droog wrote:
>
> > On May 11, 1:31 am, "io_x" <a...@b.c.invalid> wrote:
> > > "luser- -droog" <mijo...@yahoo.com> ha scritto nel messaggionews: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.
>
> > [...]
> > > > /* 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
>
> > unsigned sz;
> > (int) sz < 0
>
> > Does this just check the high bit (assuming 2's complement)?
>
> I don't know,
> but I don't see the relevance of ((int) sz < 0).
>
> If you want to check for wrap around after (sz += mem->max), then
>
>     if (sz - mem->max > sz) ThenItWrapped;

I think that's closer to what io_x was suggesting.
For my current purposes sz will be no more than 8*USHORTMAX (if there
is such a thing). But mem-max itself could conceivably get pretty big.
(~2 gigs, right? assuming 32-bit words).

If I approach this limit, I think I can squeeze a little more life out
of this if I make mem->max and mem->used 64bits (since they cover the
whole space), but change the adresses to be relative to the table
location (mtabloc, above) instead of mem->base.

I'm eager to start building on top of this module, but I want to get
as close to "right" as is practical.

[toc] | [prev] | [next] | [standalone]


#3889

FromPeter Nilsson <airia@acay.com.au>
Date2011-05-12 22:47 -0700
Message-ID<1143794b-7eeb-417a-822a-8a4343e80cc5@q12g2000prb.googlegroups.com>
In reply to#3788
On May 12, 4:05 pm, luser- -droog <mijo...@yahoo.com> wrote:
> unsigned sz;
> (int) sz < 0

Conversion of an unsigned int value not in the range of int to
an int is unspecified in C90 and (to all intents and purposes)
undefined behaviour in C99.

Also note that UINT_MAX may be more than 2 * INT_MAX + 1.

Addition of two unsigned ints a and b will 'wrap' if and only
if (a > -1u - b) [i.e. mathematically a + b > UINT_MAX.]

--
Peter

[toc] | [prev] | [next] | [standalone]


#3894

From"io_x" <a@b.c.invalid>
Date2011-05-13 10:21 +0200
Message-ID<4dcce9da$0$38646$4fafbaef@reader1.news.tin.it>
In reply to#3889
---------------------
"Peter Nilsson" <airia@acay.com.au> ha scritto nel messaggio
news:1143794b-7eeb-417a-822a-8a4343e80cc5@q12g2000prb.googlegroups.com...
On May 12, 4:05 pm, luser- -droog <mijo...@yahoo.com> wrote:
> unsigned sz;
> (int) sz < 0

Conversion of an unsigned int value not in the range of int to
an int is unspecified in C90 and (to all intents and purposes)
undefined behaviour in C99.

Also note that UINT_MAX may be more than 2 * INT_MAX + 1.

Addition of two unsigned ints a and b will 'wrap' if and only
if (a > -1u - b) [i.e. mathematically a + b > UINT_MAX.]

--
Peter
-------------------------

you can program a set of manchines

you can to program one machine

if someone has to put one value in a register
he should know how many bits that
register has

the result of program one set of machines
instead of one machine only is code more bugger
or that begin soon difficult to understand
for humans
Buona Giornata


[toc] | [prev] | [next] | [standalone]


#4290

FromTim Rentsch <txr@alumni.caltech.edu>
Date2011-05-18 22:21 -0700
Message-ID<kfnoc2zw9iu.fsf@x-alumni2.alumni.caltech.edu>
In reply to#3889
Peter Nilsson <airia@acay.com.au> writes:

> On May 12, 4:05 pm, luser- -droog <mijo...@yahoo.com> wrote:
>> unsigned sz;
>> (int) sz < 0
>
> Conversion of an unsigned int value not in the range of int to
> an int is unspecified in C90 and (to all intents and purposes)
> undefined behaviour in C99.

Correction:  in both cases the behavior is implementation defined.
In C90 that is limited to producing an implementation-defined
value.  C99 expands that just slightly to allow raising an
implementation-defined signal.  This is different from UB both in
principle and in practice:  in principle, because whatever occurs
must be documented;  in practice, because the range of behaviors
that actually occur is (and always will be) much more well-behaved
than arbitrary undefined behavior.  Invoking such behavior may not
be generally desirable, but it is not undefined behavior, not even
just to all intents and purposes.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web