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


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

Reference counting

Started bydaknok <radekslupik@gmail.com>
First post2011-06-05 12:52 +0200
Last post2011-06-16 14:42 -0400
Articles 18 on this page of 78 — 21 participants

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


Contents

  Reference counting daknok <radekslupik@gmail.com> - 2011-06-05 12:52 +0200
    Re: Reference counting Angel <angel+news@spamcop.net> - 2011-06-05 11:17 +0000
      Re: Reference counting Gene <gene.ressler@gmail.com> - 2011-06-06 20:07 -0700
        Multiple Inheritance (Was Re: Reference counting) Shao Miller <sha0.miller@gmail.com> - 2011-06-11 03:57 -0500
          Re: Multiple Inheritance (Was Re: Reference counting) Dr Nick <3-nospam@temporary-address.org.uk> - 2011-06-12 07:22 +0100
    Re: Reference counting China Blue Angels <chine.bleu@yahoo.com> - 2011-06-05 05:07 -0700
    Re: Reference counting "BartC" <bc@freeuk.com> - 2011-06-05 13:14 +0100
      Re: Reference counting daknok <daknok@example.com> - 2011-06-05 14:22 +0200
        Re: Reference counting "BartC" <bc@freeuk.com> - 2011-06-05 14:45 +0100
          Re: Reference counting daknok <daknok@example.com> - 2011-06-05 17:01 +0200
            Re: Reference counting Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-05 18:06 -0400
            Re: Reference counting Dr Nick <3-nospam@temporary-address.org.uk> - 2011-06-12 07:25 +0100
    Re: Reference counting daknok <daknok@example.com> - 2011-06-05 15:29 +0200
    Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-05 12:17 -0500
    Re: Reference counting cri@tiac.net (Richard Harter) - 2011-06-05 17:11 +0000
    Re: Reference counting ImpalerCore <jadill33@gmail.com> - 2011-06-10 11:30 -0700
      Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-10 16:04 -0400
      Re: Reference counting James Kuyper <jameskuyper@verizon.net> - 2011-06-10 22:56 -0400
        Re: Reference counting ImpalerCore <jadill33@gmail.com> - 2011-06-11 12:36 -0700
          Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-11 17:23 -0500
            Re: Reference counting luser- -droog <mijoryx@yahoo.com> - 2011-06-12 15:15 -0700
              Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-12 21:13 -0500
                Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-13 12:37 -0700
                  Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-13 12:54 -0700
                  Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-13 16:40 -0500
            Re: Reference counting ImpalerCore <jadill33@gmail.com> - 2011-06-13 07:34 -0700
              Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-13 14:26 -0500
                Re: Reference counting ImpalerCore <jadill33@gmail.com> - 2011-06-13 12:47 -0700
                  Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-13 17:33 -0500
                    Re: Reference counting ImpalerCore <jadill33@gmail.com> - 2011-06-14 12:14 -0700
                      Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-15 02:16 -0500
                    Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-15 15:34 -0700
                      Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-15 15:42 -0700
                        Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-16 13:45 -0700
                          Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-16 17:02 -0500
                            Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-16 15:18 -0700
                              Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-16 16:01 -0700
                                Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-16 18:44 -0500
                                  Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-16 17:11 -0700
                          Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-16 18:39 -0500
          Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-11 16:51 -0700
            Re: Reference counting "Chris M. Thomasson" <cristom@charter.net> - 2011-06-11 18:28 -0700
              Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-12 16:10 -0500
              Re: Reference counting ImpalerCore <jadill33@gmail.com> - 2011-06-13 07:36 -0700
        Re: Reference counting Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-14 15:00 -0700
    Re: Reference counting luser- -droog <mijoryx@yahoo.com> - 2011-06-13 21:46 -0700
      Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-14 03:19 -0500
        Re: Reference counting Angel <angel+news@spamcop.net> - 2011-06-14 07:31 +0000
          Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-14 05:02 -0500
          Re: Reference counting Keith Thompson <kst-u@mib.org> - 2011-06-14 09:03 -0700
            Re: Reference counting luser- -droog <mijoryx@yahoo.com> - 2011-06-14 10:23 -0700
              Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-14 13:46 -0500
            Re: Reference counting Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-14 15:05 -0700
              Re: Reference counting Keith Thompson <kst-u@mib.org> - 2011-06-14 16:44 -0700
                Re: Reference counting Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-15 06:54 -0700
                  Re: Reference counting Keith Thompson <kst-u@mib.org> - 2011-06-15 08:46 -0700
                    Re: Reference counting pete <pfiland@mindspring.com> - 2011-06-15 22:19 -0400
                      Re: Reference counting James Kuyper <jameskuyper@verizon.net> - 2011-06-15 23:03 -0400
                    Re: Reference counting Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-19 14:48 -0700
          Re: Reference counting Seebs <usenet-nospam@seebs.net> - 2011-06-14 17:16 +0000
      Re: Reference counting luser- -droog <mijoryx@yahoo.com> - 2011-06-14 20:53 -0700
        Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-16 18:49 -0500
    Re: Reference counting "io_x" <a@b.c.invalid> - 2011-06-14 08:30 +0200
      Re: Reference counting "io_x" <a@b.c.invalid> - 2011-06-14 09:15 +0200
        Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-14 04:21 -0500
        Re: Reference counting "io_x" <a@b.c.invalid> - 2011-06-16 10:35 +0200
          Re: Reference counting Ian Collins <ian-news@hotmail.com> - 2011-06-16 20:36 +1200
          Re: Reference counting "io_x" <a@b.c.invalid> - 2011-06-16 11:21 +0200
            Re: Reference counting "io_x" <a@b.c.invalid> - 2011-06-19 17:57 +0200
            Re: Reference counting "io_x" <a@b.c.invalid> - 2011-06-19 20:01 +0200
              Re: Reference counting Keith Thompson <kst-u@mib.org> - 2011-06-19 11:07 -0700
              Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-19 23:15 -0500
                Re: Reference counting "io_x" <a@b.c.invalid> - 2011-06-20 08:21 +0200
          Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-16 19:05 -0500
            Re: Reference counting Ian Collins <ian-news@hotmail.com> - 2011-06-17 12:09 +1200
    Re: Reference counting luser- -droog <mijoryx@yahoo.com> - 2011-06-15 08:40 -0700
    Re: Reference counting "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-06-16 11:08 -0700
      Re: Reference counting Shao Miller <sha0.miller@gmail.com> - 2011-06-16 14:42 -0400

Page 4 of 4 — ← Prev page 1 2 3 [4]


#6091

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-06-14 20:53 -0700
Message-ID<7cca148c-37c5-473e-9fb6-8bbf240fe14d@f2g2000yqh.googlegroups.com>
In reply to#6012
luser- -droog wrote:
> An external table would avoid all the alignment-guessing for pre-C1X.
> I'm too lazy to finish coding this, but what about a simple open-
> addressed
> hash-table?

Laziness overcome!

517(0)10:47 PM:~ 0> cat rfct.c
#define TEST

enum { MAX = 100, TABSZ = 2*MAX+1 };

#include <stdlib.h>

struct ref_ {
    void *ptr;
    int count;
    void (*final)(void *ptr);
} typedef ref_;

ref_ ref_tab_[TABSZ];

void ref_init(void) {
    int i;
    for (i=0; i < TABSZ; i++) {
        ref_tab_[i] = (ref_){ .ptr = NULL, .count = 0, .final =
NULL };
    }
}

/* courtesy of Shao Miller */
   unsigned char ptr_hash(void * ptr) {
       unsigned char
         * pos = (void *)&ptr,
         * end = pos + sizeof ptr,
         result = 0;
       while (pos < end)
         result ^= *pos++;
       return result;
     }

   /* pete says UB when 'sizeof (int) == 1' */
/*   static xxx ptr_hash_table[1 << CHAR_BIT]; */

ref_ *ref_find(void *ptr) {
    int
        h = ptr_hash(ptr) % TABSZ,
        i = h;
    if (ptr == ref_tab_[i].ptr || ref_tab_[i].ptr == NULL)
        return &ref_tab_[i];
    for (++i; i < TABSZ; i++) {
        if (ptr == ref_tab_[i].ptr || ref_tab_[i].ptr == NULL)
            return &ref_tab_[i];
    }
    for (i = 0; i < h; i++) {
        if (ptr == ref_tab_[i].ptr || ref_tab_[i].ptr == NULL)
            return &ref_tab_[i];
    }
    return NULL;
}

void ref_new(void *ptr) {
    ref_ *ref = ref_find(ptr);
    if (ref) {
        ref->ptr = ptr;
        ref->count = 1;
        ref->final = free;
    }
}

ref_ *ref_get(void *ptr) {
    ref_ *ref = ref_find(ptr);
    if (ref)
        if (ref->ptr == NULL)
            return NULL;
    return ref;
}

void *ref_malloc(size_t sz) {
    void *ptr = malloc(sz);
    if (ptr)
        ref_new(ptr);
    return ptr;
}

void ref_inc(void *ptr) {
    ref_ *ref = ref_get(ptr);
    if (ref) ref->count++;
}

void ref_dec(void *ptr) {
    ref_ *ref = ref_get(ptr);
    if (ref)
        if (--ref->count == 0) {
            ref->final(ptr);
            ref->ptr = 0;
            ref->final = NULL;
        }
}

void ref_set_final(void *ptr, void (*final)(void *ptr)) {
    ref_ *ref = ref_get(ptr);
    if (ref)
        ref->final = final;
}

#ifdef TEST
#include <string.h>
#include <stdio.h>

#define strdup(s) strcpy(ref_malloc(sizeof(s)),s)

void print(char *s) {
    ref_inc(s);
    if (ref_get(s)) printf("%s\n", s);
    ref_dec(s);
}

int main(void) {
    ref_init();
    char *s = strdup("apple");
    char *t = strdup("banana");

    print(s); /* apple */
    print(t); /* banana */

    ref_dec(s);
    print(s); /* */
    print(t); /* banana */

    ref_dec(t);
    print(s); /* */
    print(t); /* */

    return 0;
}

#endif
518(0)10:47 PM:~ 0> make rfct && rfct
cc -g -Wall   -c -o rfct.o rfct.c
cc   rfct.o   -o rfct
apple
banana
banana
519(0)10:47 PM:~ 0>

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


#6201

FromShao Miller <sha0.miller@gmail.com>
Date2011-06-16 18:49 -0500
Message-ID<ite4q8$8o1$1@dont-email.me>
In reply to#6091
On 6/14/2011 10:53 PM, luser- -droog wrote:
> luser- -droog wrote:
>> An external table would avoid all the alignment-guessing for pre-C1X.
>> I'm too lazy to finish coding this, but what about a simple open-
>> addressed
>> hash-table?
>
> Laziness overcome!
>
> [...code...]
>
> int main(void) {
>      ref_init();
>      char *s = strdup("apple");
>      char *t = strdup("banana");
>
>      print(s); /* apple */
>      print(t); /* banana */
>
>      ref_dec(s);
>      print(s); /* */
>      print(t); /* banana */
>
>      ref_dec(t);
>      print(s); /* */
>      print(t); /* */
>
>      return 0;
> }
>
> #endif
> 518(0)10:47 PM:~ 0>  make rfct&&  rfct
> cc -g -Wall   -c -o rfct.o rfct.c
> cc   rfct.o   -o rfct
> apple
> banana
> banana
> 519(0)10:47 PM:~ 0>

How interesting!  You impose no special requirements on the programmers 
to use overriding macros or wrapper functions for allocation functions, 
you do not touch nor examine the state of the pointed-to objects, yet 
your 'ref_dec' invocations behave differently!  Pretty nifty.

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


#6037

From"io_x" <a@b.c.invalid>
Date2011-06-14 08:30 +0200
Message-ID<4df6ff99$0$2687$4fafbaef@reader1.news.tin.it>
In reply to#5477
"daknok" <radekslupik@gmail.com> ha scritto nel messaggio
news:4deb6008$0$28457$703f8584@textnews.kpn.nl...
> Hi,
>
> If I want to do simple reference counting for different structures I would go
> like this:
>
> struct String {
>    unsigned references;
>    char *cString;
>    size_t length;
> };
>
> void StringRelease(struct String *str) {
>    str->references--;
>    if (str->references == 0) {
>        free(str->cString);
>        free(str);
>    }
> }
>
> struct String *StringRetain(struct String *str) {
>    str->references++;
>    return str;
> }

if i would write something as that:
1) i not would write a wrapper to malloc
2) i not would write the referenece counter in the
   struct because it waste space
3) for each struct has need referece counter i would write
   one global vaiable as in:

int StringCont;
struct String {
    char *cString;
    size_t length;
 };

int  vvvCont;
struct vvv{
    char  *cg;
    int    le;
 };

struct String*  CostruttoreString(char*  what)
{struct String    *a;
 char             *b;
 unsigned       c, i;

 a=malloc(sizeof(String));
 if(a==0) return 0;
 c=(what==0? 0: strlen(what));
 if((int)c<0) {m: free(a); return 0;}
 b=malloc(c+2);
 if(b==0) goto m;
 for(i=0; i<c; ++i)
      b[i]=what[i];
 b[i]=0;
 a.cString=b;
 ++StringCont;
 if(StringCont<=0)
    {printf("overflow string conter\n");
     exit(0);
    }
  a.lenght=c+2;
  return a;
 }

void  DistruttoreString(struct String*  what)
{char  *b;
 if(what==0) return;
 free(what.cString);
 free(what);
 --StringCont;
 if(StringCont<0)
    {printf("overflow string conter\n");
     exit(0);
    }
}

there is some problem in multi thread program
for access to the varible counter, so in that case use some
Os sincronizzation

> Though if I have about ten structs, I have ten different functions to increase
> and decrease the reference count. Is it, in some way, possible to just provide
> one function for increasing and one function for decreasing the reference
> count which can be used for all structs with a reference count?
>


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


#6042

From"io_x" <a@b.c.invalid>
Date2011-06-14 09:15 +0200
Message-ID<4df70a29$0$2690$4fafbaef@reader1.news.tin.it>
In reply to#6037
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4df6ff99$0$2687$4fafbaef@reader1.news.tin.it...
> struct String*  CostruttoreString(char*  what)
> {struct String    *a;
> char             *b;
> unsigned       c, i;
>
> a=malloc(sizeof(String));
> if(a==0) return 0;
> c=(what==0? 0: strlen(what));
> if((int)c<0) {m: free(a); return 0;}
> b=malloc(c+2);
> if(b==0) goto m;
> for(i=0; i<c; ++i)
>      b[i]=what[i];
better
if(what!=0)
    {for(i=0; i<c; ++i)
         b[i]=what[i];
    }
else b[0]=0;

> b[i]=0;
> a.cString=b;
> ++StringCont;
> if(StringCont<=0)
>    {printf("overflow string conter\n");
>     exit(0);
>    }
>  a.lenght=c+2;
>  return a;
> }


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


#6049

FromShao Miller <sha0.miller@gmail.com>
Date2011-06-14 04:21 -0500
Message-ID<it75mv$43b$1@dont-email.me>
In reply to#6042
On 6/14/2011 2:15 AM, io_x wrote:
> "io_x"<a@b.c.invalid>  ha scritto nel messaggio
> news:4df6ff99$0$2687$4fafbaef@reader1.news.tin.it...
>> struct String*  CostruttoreString(char*  what)
>> {struct String    *a;
>> char             *b;
>> unsigned       c, i;
>>
>> a=malloc(sizeof(String));
>> [...more code...]

I think you meant 'sizeof (struct String)' there. :)

I don't completely understand how your code accomplishes reference 
counting.  Your code appears to keep track of how many strings have been 
allocated.  But I think that the "reference counting" subject of this 
thread is more along the lines of:
- You allocate an object and assume that there is one user of that object.
- Another user can subscribe for use of the object (increment the 
reference count of that object).
- A user can unsubscribe ("release") from use of the object (decrement 
the reference count of the object).
- When there are no more users of the object, the object can be freed.

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


#6141

From"io_x" <a@b.c.invalid>
Date2011-06-16 10:35 +0200
Message-ID<4df9c004$0$40246$4fafbaef@reader1.news.tin.it>
In reply to#6042
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4df70a29$0$2690$4fafbaef@reader1.news.tin.it...

what about my try?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// macro for types
#define  u64  uint64_t
#define  u32  uint32_t
#define  u16  uint16_t
#define  u8    uint8_t
#define  uns  unsigned
#define  dbl    double

// macro for function
#define  P  printf

// macro for keyWords
#define G  goto
#define R  return
#define W  while
#define F  for

union  tipidiarray{
  double  a;
  size_t  v;
  u8*     m;
};

/* suppone che gli array di double, size_t
 u8* si mantengano allineati */
u32   elemento=sizeof(union  tipidiarray);

u8*  IniMem(u32  s)
{u8     *p;
 u32   *pu;
 u32     c;

 if(s==0)  R  0;
 c=s;        c+=elemento;
 if(c<s)   R  0;
 p=malloc(c);
 if(p==0)  R  0;
 pu=(u32*)p; p+=elemento;
 pu[0]=1; pu[1]=(u32)p;
 return  p;
}

u8*  GetRef(u8*  v)
{u8     *p;
 u32   *pu;

 if(v==0)                    R  0;
 p=v-elemento; pu=(u32*)p;
 if(pu[0]==0||pu[1]!=(u32)v) R  0;
 ++pu[0];
 if(pu[0]==0){--pu[0];       R  0;}
 R  v;
}

u32 GetRefCounter(u8*  v)
{u8     *p;
 u32   *pu;

 if(v==0)          R        0;
 p=v-elemento; pu=(u32*)p;
 if(pu[1]!=(u32)v) R  (u32)-1;
 R  pu[0];
}

u8* PutRef(u32* ris, u8*   v)
{u8     *p;
 u32   *pu;

 if(ris==0||v==0)  R  0;
 *ris=0;
 p=v-elemento; pu=(u32*)p;
 if(pu[0]==0||pu[1]!=(u32)v)
      {*ris=-1; R  0;}
 --pu[0];
 if(pu[0]==0)
      { pu[1]=0; free(p); }
 R  0;
}

int  main(void)
{double  *arrd, *pad;
 u32     *arru, *elm, ris;

 if(2*sizeof(u32)>elemento )
   {P("Errore di inizializzazione\n"); R  0;}
 arrd=(double*)IniMem(10*sizeof(double));
 arrd[0]=1.111;
 pad=(dbl*)GetRef((u8*)arrd);
 if(pad==0) {P("Errore della funzione GetRef()\n");
             P("o memoria insufficiente\n");
             exit(0);
            }
 P("val=%f nref=%u\n", pad[0], (uns)GetRefCounter((u8*)arrd));
 pad=(dbl*)PutRef(&ris, (u8*)pad);
 P("val=%f nref=%u pad=%p ris=%u\n",
    arrd[0], (uns)GetRefCounter((u8*)arrd), (u8*)pad, (uns) ris);

 arrd=(dbl*)PutRef(&ris, (u8*)pad);
 P("nref=%u ris=%u\n", (uns)GetRefCounter((u8*)arrd), (uns) ris);
 R   0;
}




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


#6142

FromIan Collins <ian-news@hotmail.com>
Date2011-06-16 20:36 +1200
Message-ID<95ttl1Fd2pU8@mid.individual.net>
In reply to#6141
On 06/16/11 08:35 PM, io_x wrote:
> "io_x"<a@b.c.invalid>  ha scritto nel messaggio
> news:4df70a29$0$2690$4fafbaef@reader1.news.tin.it...
>
> what about my try?
>
> #include<stdio.h>
> #include<stdlib.h>
> #include<stdint.h>
>
> // macro for types
> #define  u64  uint64_t
> #define  u32  uint32_t
> #define  u16  uint16_t
> #define  u8    uint8_t
> #define  uns  unsigned
> #define  dbl    double
>
> // macro for function
> #define  P  printf

Sane readers will have given up by here.

-- 
Ian Collins

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


#6144

From"io_x" <a@b.c.invalid>
Date2011-06-16 11:21 +0200
Message-ID<4df9caab$0$40251$4fafbaef@reader1.news.tin.it>
In reply to#6141
"io_x" <a@b.c.invalid> ha scritto nel messaggio 
news:4df9c004$0$40246$4fafbaef@reader1.news.tin.it...
>
> "io_x" <a@b.c.invalid> ha scritto nel messaggio
> news:4df70a29$0$2690$4fafbaef@reader1.news.tin.it...
>
> what about my try?

someone think there are too many cast?
what about consider for align
union  tipidiarray{
  double  a;
  size_t  v;
  u8*     m;
};
and think are possible array of each element of that union?
so if "a" is aligned to double, size_t, u8*
"a"+sizeof(double), "a"+sizeof(size_t)
"a"+sizeof(u8*) has to be aligned to each rispettive type
and so if "a" is a return address of malloc
"a"+sizeof(tipidiarray)
is ok for double, size_t, u8*
[if "a" point to sufficient memory]

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// macro for types
#define  u64  uint64_t
#define  u32  uint32_t
#define  u16  uint16_t
#define  u8    uint8_t
#define  uns  unsigned
#define  dbl    double

// macro for function
#define  P  printf

// macro for keyWords
#define G  goto
#define R  return
#define W  while
#define F  for

union  tipidiarray{
  double  a;
  size_t  v;
  u8*     m;
};

u32   elemento=sizeof(union  tipidiarray);

u8*  IniMem(u32  s)
{u8     *p;
 u32   *pu;
 u32     c;

 if(s==0)  R  0;
 c=s;        c+=elemento;
 if(c<=s)  R  0;
 p=malloc(c);
 if(p==0)  R  0;
 pu=(u32*)p; p+=elemento;
 pu[0]=1; pu[1]=(u32)p;
 R  p;
}

u8*  GetRef(u8*  v)
{u8     *p;
 u32   *pu;

 if(v==0)                    R  0;
 p=v-elemento; pu=(u32*)p;
 if(pu[0]==0||pu[1]!=(u32)v) R  0;
 ++pu[0];
 if(pu[0]==0){--pu[0];       R  0;}
 R  v;
}

u32 GetRefCounter(u8*  v)
{u8     *p;
 u32   *pu;

 if(v==0)          R        0;
 p=v-elemento; pu=(u32*)p;
 if(pu[1]!=(u32)v) R  (u32)-1;
 R  pu[0];
}

u8* PutRef(u32* ris, u8*   v)
{u8     *p;
 u32   *pu;

 if(ris==0) R  0;
 *ris=0;
 if(v==0)   R  0;
 p=v-elemento; pu=(u32*)p;
 if(pu[0]==0||pu[1]!=(u32)v)
      {*ris=-1; R  0;}
 --pu[0];
 if(pu[0]==0)
      { pu[1]=0; free(p); }
 R  0;
}

int  main(void)
{double  *arrd, *pad;
 u32     *arru, *elm, ris;

 if(2*sizeof(u32)>elemento )
   {P("Errore di inizializzazione\n"); R  0;}
 arrd=(double*)IniMem(10*sizeof(double));
 arrd[0]=1.111;
 pad=(double*)GetRef((u8*)arrd);
 if(pad==0) {P("Errore della funzione GetRef()\n");
             P("o memoria insufficiente\n");
             exit(0);
            }
 P("val=%f nref=%u\n", pad[0], (uns)GetRefCounter((u8*)arrd));
 pad=(dbl*)PutRef(&ris, (u8*)pad);
 P("val=%f nref=%u pad=%p ris=%u\n",
    arrd[0], (uns)GetRefCounter((u8*)arrd), (u8*)pad, (uns) ris);

 arrd=(double*)PutRef(&ris, (u8*)pad);
 P("nref=%u ris=%u\n", (uns)GetRefCounter((u8*)arrd), (uns) ris);
 R   0;
}



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


#6451

From"io_x" <a@b.c.invalid>
Date2011-06-19 17:57 +0200
Message-ID<4dfe1c27$0$15665$4fafbaef@reader2.news.tin.it>
In reply to#6144
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4df9caab$0$40251$4fafbaef@reader1.news.tin.it...
>
> "io_x" <a@b.c.invalid> ha scritto nel messaggio
> news:4df9c004$0$40246$4fafbaef@reader1.news.tin.it...
>>
>> "io_x" <a@b.c.invalid> ha scritto nel messaggio
>> news:4df70a29$0$2690$4fafbaef@reader1.news.tin.it...
>>
>> what about my try?
>
> someone think there are too many cast?
> what about consider for align
> union  tipidiarray{
>  double  a;
>  size_t  v;
>  u8*     m;
> };
> and think are possible array of each element of that union?
> so if "a" is aligned to double, size_t, u8*
> "a"+sizeof(double), "a"+sizeof(size_t)
> "a"+sizeof(u8*) has to be aligned to each rispettive type
> and so if "a" is a return address of malloc
> "a"+sizeof(tipidiarray)
> is ok for double, size_t, u8*
> [if "a" point to sufficient memory]

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// macro for types
#define  u64  uint64_t
#define  u32  uint32_t
#define  u16  uint16_t
#define  u8    uint8_t
#define  uns  unsigned
#define  dbl    double

// macro for function
#define  P  printf

// macro for keyWords
#define G  goto
#define R  return
#define W  while
#define F  for


u32  ele(void)
{double        dbt[2];
 size_t        szt[2];
 u8*           art[2], *a, *b;
 long unsigned lut[2];
 u32             r, v;

 a=&dbt[0];  b=&dbt[1];
           r=b-a;
 a=&szt[0];  b=&szt[1];
 if(b-a>r) r=b-a;
 a=&art[0];  b=&art[1];
 if(b-a>r) r=b-a;
 a=&lut[0];  b=&lut[1];
 if(b-a>r) r=b-a;
 R  r;
}

union  tipidiarray{
  double  a;
  size_t  v;
  u8*     m;
};

u32   elemento;

u8*  IniMem(u32  s)
{u8     *p;
 u32   *pu;
 u32     c;

 if(s==0)  R  0;
 c=s;        c+=elemento;
 if(c<=s)  R  0;
 p=malloc(c);
 if(p==0)  R  0;
 pu=(u32*)p; p+=elemento;
 pu[0]=1; pu[1]=(u32)p;
 R  p;
}

u8*  GetRef(u8*  v)
{u8     *p;
 u32   *pu;

 if(v==0)                    R  0;
 p=v-elemento; pu=(u32*)p;
 if(pu[0]==0||pu[1]!=(u32)v) R  0;
 ++pu[0];
 if(pu[0]==0){--pu[0];       R  0;}
 R  v;
}

u32 GetRefCounter(u8*  v)
{u8     *p;
 u32   *pu;

 if(v==0)          R        0;
 p=v-elemento; pu=(u32*)p;
 if(pu[1]!=(u32)v) R  (u32)-1;
 R  pu[0];
}

u8* PutRef(u32* ris, u8*   v)
{u8     *p;
 u32   *pu;

 if(ris==0) R  0;
 *ris=0;
 if(v==0)   R  0;
 p=v-elemento; pu=(u32*)p;
 if(pu[0]==0||pu[1]!=(u32)v)
      {*ris=-1; R  0;}
 --pu[0];
 if(pu[0]==0)
      { pu[1]=0; free(p); }
 R  0;
}

int  main(void)
{double  *arrd, *pad;
 u32     *arru, *elm, ris;

 elemento=ele();
 P("Elemento=%u\n", elemento);
 if(2*sizeof(u32)>elemento )
   {P("Errore di inizializzazione\n"); R  0;}
 arrd=(double*)IniMem(10*sizeof(double));
 arrd[0]=1.111;
 pad=(double*)GetRef((u8*)arrd);
 if(pad==0) {P("Errore della funzione GetRef()\n");
             P("o memoria insufficiente\n");
             exit(0);
            }
 P("val=%f nref=%u\n", pad[0], (uns)GetRefCounter((u8*)arrd));
 pad=(dbl*)PutRef(&ris, (u8*)pad);
 P("val=%f nref=%u pad=%p ris=%u\n",
    arrd[0], (uns)GetRefCounter((u8*)arrd), (u8*)pad, (uns) ris);

 arrd=(double*)PutRef(&ris, (u8*)pad);
 P("nref=%u ris=%u\n", (uns)GetRefCounter((u8*)arrd), (uns) ris);
 R   0;
}




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


#6454

From"io_x" <a@b.c.invalid>
Date2011-06-19 20:01 +0200
Message-ID<4dfe3933$0$15666$4fafbaef@reader2.news.tin.it>
In reply to#6144
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4df9caab$0$40251$4fafbaef@reader1.news.tin.it...
>
> "io_x" <a@b.c.invalid> ha scritto nel messaggio
> news:4df9c004$0$40246$4fafbaef@reader1.news.tin.it...
>>
>> "io_x" <a@b.c.invalid> ha scritto nel messaggio
>> news:4df70a29$0$2690$4fafbaef@reader1.news.tin.it...
>>
>> what about my try?
>
> someone think there are too many cast?
> what about consider for align
> union  tipidiarray{
>  double  a;
>  size_t  v;
>  u8*     m;
> };
> and think are possible array of each element of that union?
> so if "a" is aligned to double, size_t, u8*
> "a"+sizeof(double), "a"+sizeof(size_t)
> "a"+sizeof(u8*) has to be aligned to each rispettive type
> and so if "a" is a return address of malloc
> "a"+sizeof(tipidiarray)
> is ok for double, size_t, u8*
> [if "a" point to sufficient memory]

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// macro for types
#define  u64  uint64_t
#define  u32  uint32_t
#define  u16  uint16_t
#define  u8    uint8_t
#define  uns  unsigned
#define  dbl    double

// macro for function
#define  P  printf

// macro for keyWords
#define G  goto
#define R  return
#define W  while
#define F  for

/*
 ??????????????
 serve per trovare il prossimo elemento
 allocato da malloc con allineamento
 buono per tutti i tipi possibili nella
 macchina in cui gira ma tale che l'allineamento
 sia del tipo: + (0, 1, 2, 4, 8, 16, 32, 64..)

*/
u32  ele(void)
{long double  ldbt[2];
 double        dbt[2];
 size_t        szt[2];
 u8*           art[2], *a, *b;
 long unsigned lut[2];
 u32             r, v;

 r=0;
 a=&ldbt[0];  b=&ldbt[1];
 v=b-a;
// 100b-1b=11
 if(((v-1)&v)==0 && v>r)  r=v;

 a=&dbt[0];  b=&dbt[1];
 v=b-a;
// 100b-1b=11
 if(((v-1)&v)==0 && v>r)  r=v;

 a=&szt[0];  b=&szt[1];
 v=b-a;
 if(((v-1)&v)==0 && v>r)  r=v;

 a=&art[0];  b=&art[1];
 v=b-a;
 if(((v-1)&v)==0 && v>r)  r=v;

 a=&lut[0];  b=&lut[1];
 v=b-a;
 if(((v-1)&v)==0 && v>r)  r=v;

 R  r;
}

union  tipidiarray{
  double  a;
  size_t  v;
  u8*     m;
};

u32   elemento;

u8*  IniMem(u32  s)
{u8     *p;
 u32   *pu;
 u32     c;

 if(s==0)  R  0;
 c=s;        c+=elemento;
 if(c<=s)  R  0;
 p=malloc(c);
 if(p==0)  R  0;
 pu=(u32*)p; p+=elemento;
 pu[0]=1; pu[1]=(u32)p;
 R  p;
}

u8*  GetRef(u8*  v)
{u8     *p;
 u32   *pu;

 if(v==0)                    R  0;
 p=v-elemento; pu=(u32*)p;
 if(pu[0]==0||pu[1]!=(u32)v) R  0;
 ++pu[0];
 if(pu[0]==0){--pu[0];       R  0;}
 R  v;
}

u32 GetRefCounter(u8*  v)
{u8     *p;
 u32   *pu;

 if(v==0)          R        0;
 p=v-elemento; pu=(u32*)p;
 if(pu[1]!=(u32)v) R  (u32)-1;
 R  pu[0];
}

u8* PutRef(u32* ris, u8*   v)
{u8     *p;
 u32   *pu;

 if(ris==0) R  0;
 *ris=0;
 if(v==0)   R  0;
 p=v-elemento; pu=(u32*)p;
 if(pu[0]==0||pu[1]!=(u32)v)
      {*ris=-1; R  0;}
 --pu[0];
 if(pu[0]==0)
      { pu[1]=0; free(p); }
 R  0;
}

int  main(void)
{double  *arrd, *pad;
 u32     *arru, *elm, ris;

 elemento=ele();
 P("Elemento=%u\n", elemento);
 if(2*sizeof(u32)>elemento )
   {P("Errore di inizializzazione\n"); R  0;}
 arrd=(double*)IniMem(10*sizeof(double));
 arrd[0]=1.111;
 pad=(double*)GetRef((u8*)arrd);
 if(pad==0) {P("Errore della funzione GetRef()\n");
             P("o memoria insufficiente\n");
             exit(0);
            }
 P("val=%f nref=%u\n", pad[0], (uns)GetRefCounter((u8*)arrd));
 pad=(dbl*)PutRef(&ris, (u8*)pad);
 P("val=%f nref=%u pad=%p ris=%u\n",
    arrd[0], (uns)GetRefCounter((u8*)arrd), (u8*)pad, (uns) ris);

 arrd=(double*)PutRef(&ris, (u8*)pad);
 P("nref=%u ris=%u\n", (uns)GetRefCounter((u8*)arrd), (uns) ris);
 R   0;
}





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


#6455

FromKeith Thompson <kst-u@mib.org>
Date2011-06-19 11:07 -0700
Message-ID<ln39j5y9tk.fsf@nuthaus.mib.org>
In reply to#6454
"io_x" <a@b.c.invalid> writes:
[...]
> // macro for types
> #define  u64  uint64_t
> #define  u32  uint32_t
> #define  u16  uint16_t
> #define  u8    uint8_t
> #define  uns  unsigned
> #define  dbl    double
>
> // macro for function
> #define  P  printf
>
> // macro for keyWords
> #define G  goto
> #define R  return
> #define W  while
> #define F  for

No.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


#6485

FromShao Miller <sha0.miller@gmail.com>
Date2011-06-19 23:15 -0500
Message-ID<itme0r$h0m$1@dont-email.me>
In reply to#6454
On 6/19/2011 1:01 PM, io_x wrote:
> "io_x"<a@b.c.invalid>  ha scritto nel messaggio
> news:4df9caab$0$40251$4fafbaef@reader1.news.tin.it...
>>
>> "io_x"<a@b.c.invalid>  ha scritto nel messaggio
>> news:4df9c004$0$40246$4fafbaef@reader1.news.tin.it...
>>>
>>> "io_x"<a@b.c.invalid>  ha scritto nel messaggio
>>> news:4df70a29$0$2690$4fafbaef@reader1.news.tin.it...
>>>
>>> what about my try?
>>
>> someone think there are too many cast?
>> what about consider for align
>> union  tipidiarray{
>>   double  a;
>>   size_t  v;
>>   u8*     m;
>> };
>> and think are possible array of each element of that union?
>> so if "a" is aligned to double, size_t, u8*
>> "a"+sizeof(double), "a"+sizeof(size_t)
>> "a"+sizeof(u8*) has to be aligned to each rispettive type
>> and so if "a" is a return address of malloc
>> "a"+sizeof(tipidiarray)
>> is ok for double, size_t, u8*
>> [if "a" point to sufficient memory]
>
> [...code...]

On 6/19/2011 1:01 PM, io_x could have written:
> #include <stdio.h>
> #include <stdlib.h>
> #include <stdint.h>
>
> uint32_t ele(void)
> {
> 	long double ldbt[2];
> 	double dbt[2];
> 	size_t szt[2];
> 	uint8_t *art[2], *a, *b;
> 	long unsigned lut[2];
> 	uint32_t r, v;
>
> 	r = 0;
> 	a = &ldbt[0];
> 	b = &ldbt[1];
> 	v = b - a;
> 	#if 0
> 	100b-1b=11
> 	#endif
> 	if (((v - 1) & v) == 0 && v > r)
> 		r = v;
>
> 	a = &dbt[0];
> 	b = &dbt[1];
> 	v = b - a;
> 	#if 0
> 	100b-1b=11
> 	#endif
> 	if (((v - 1) & v) == 0 && v > r)
> 		r = v;
>
> 	a = &szt[0];
> 	b = &szt[1];
> 	v = b - a;
> 	if (((v - 1) & v) == 0 && v > r)
> 		r = v;
>
> 	a = &art[0];
> 	b = &art[1];
> 	v = b - a;
> 	if (((v - 1) & v) == 0 && v > r)
> 		r = v;
>
> 	a = &lut[0];
> 	b = &lut[1];
> 	v = b - a;
> 	if (((v - 1) & v) == 0 && v > r)
> 		r = v;
>
> 	return r;
> }
>
> union tipidiarray {
> 	double a;
> 	size_t v;
> 	uint8_t *m;
> };
>
> uint32_t elemento;
>
> uint8_t *IniMem(uint32_t s)
> {
> 	uint8_t *p;
> 	uint32_t *pu;
> 	uint32_t c;
>
> 	if (s == 0)
> 		return 0;
> 	c = s;
> 	c += elemento;
> 	if (c <= s)
> 		return 0;
> 	p = malloc(c);
> 	if (p == 0)
> 		return 0;
> 	pu = (uint32_t *) p;
> 	p += elemento;
> 	pu[0] = 1;
> 	pu[1] = (uint32_t) p;
> 	return p;
> }
>
> uint8_t *GetRef(uint8_t * v)
> {
> 	uint8_t *p;
> 	uint32_t *pu;
>
> 	if (v == 0)
> 		return 0;
> 	p = v - elemento;
> 	pu = (uint32_t *) p;
> 	if (pu[0] == 0 || pu[1] != (uint32_t) v)
> 		return 0;
> 	++pu[0];
> 	if (pu[0] == 0) {
> 		--pu[0];
> 		return 0;
> 	}
> 	return v;
> }
>
> uint32_t GetRefCounter(uint8_t * v)
> {
> 	uint8_t *p;
> 	uint32_t *pu;
>
> 	if (v == 0)
> 		return 0;
> 	p = v - elemento;
> 	pu = (uint32_t *) p;
> 	if (pu[1] != (uint32_t) v)
> 		return (uint32_t) - 1;
> 	return pu[0];
> }
>
> uint8_t *PutRef(uint32_t * ris, uint8_t * v)
> {
> 	uint8_t *p;
> 	uint32_t *pu;
>
> 	if (ris == 0)
> 		return 0;
> 	*ris = 0;
> 	if (v == 0)
> 		return 0;
> 	p = v - elemento;
> 	pu = (uint32_t *) p;
> 	if (pu[0] == 0 || pu[1] != (uint32_t) v) {
> 		*ris = -1;
> 		return 0;
> 	}
> 	--pu[0];
> 	if (pu[0] == 0) {
> 		pu[1] = 0;
> 		free(p);
> 	}
> 	return 0;
> }
>
> int main(void)
> {
> 	double *arrd, *pad;
> 	uint32_t *arru, *elm, ris;
>
> 	elemento = ele();
> 	printf("Elemento=%u\n", elemento);
> 	if (2 * sizeof(uint32_t) > elemento) {
> 		printf("Errore di inizializzazione\n");
> 		return 0;
> 	}
> 	arrd = (double *)IniMem(10 * sizeof(double));
> 	arrd[0] = 1.111;
> 	pad = (double *)GetRef((uint8_t *) arrd);
> 	if (pad == 0) {
> 		printf("Errore della funzione GetRef()\n");
> 		printf("o memoria insufficiente\n");
> 		exit(0);
> 	}
> 	printf("val=%f nref=%u\n", pad[0],
> 	       (unsigned)GetRefCounter((uint8_t *) arrd));
> 	pad = (double *)PutRef(&ris, (uint8_t *) pad);
> 	printf("val=%f nref=%u pad=%p ris=%u\n",
> 	       arrd[0], (unsigned)GetRefCounter((uint8_t *) arrd),
> 	       (uint8_t *) pad, (unsigned)ris);
>
> 	arrd = (double *)PutRef(&ris, (uint8_t *) pad);
> 	printf("nref=%u ris=%u\n", (unsigned)GetRefCounter((uint8_t *) arrd),
> 	       (unsigned)ris);
> 	return 0;
> }

GCC gave:

io_x2.c: In function 'ele':
io_x2.c:15: warning: assignment from incompatible pointer type
io_x2.c:16: warning: assignment from incompatible pointer type
io_x2.c:24: warning: assignment from incompatible pointer type
io_x2.c:25: warning: assignment from incompatible pointer type
io_x2.c:33: warning: assignment from incompatible pointer type
io_x2.c:34: warning: assignment from incompatible pointer type
io_x2.c:39: warning: assignment from incompatible pointer type
io_x2.c:40: warning: assignment from incompatible pointer type
io_x2.c:45: warning: assignment from incompatible pointer type
io_x2.c:46: warning: assignment from incompatible pointer type

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


#6493

From"io_x" <a@b.c.invalid>
Date2011-06-20 08:21 +0200
Message-ID<4dfee692$0$44198$4fafbaef@reader1.news.tin.it>
In reply to#6485
"Shao Miller" <sha0.miller@gmail.com> ha scritto nel messaggio
news:itme0r$h0m$1@dont-email.me...
> On 6/19/2011 1:01 PM, io_x wrote:
>> "io_x"<a@b.c.invalid>  ha scritto nel messaggio
>> news:4df9caab$0$40251$4fafbaef@reader1.news.tin.it...
>> uint32_t ele(void)
>> {
>> long double ldbt[2];
>> double dbt[2];
>> size_t szt[2];
>> uint8_t *art[2], *a, *b;
>> long unsigned lut[2];
>> uint32_t r, v;
>>
>> r = 0;
>> a = &ldbt[0];
>> b = &ldbt[1];
>> v = b - a;
>> #if 0
>> 100b-1b=11
>> #endif
>> if (((v - 1) & v) == 0 && v > r)
>> r = v;
>>
>> a = &dbt[0];
>> b = &dbt[1];
>> v = b - a;
>> #if 0
>> 100b-1b=11
>> #endif
>> if (((v - 1) & v) == 0 && v > r)
>> r = v;
>>
>> a = &szt[0];
>> b = &szt[1];
>> v = b - a;
>> if (((v - 1) & v) == 0 && v > r)
>> r = v;
>>
>> a = &art[0];
>> b = &art[1];
>> v = b - a;
>> if (((v - 1) & v) == 0 && v > r)
>> r = v;
>>
>> a = &lut[0];
>> b = &lut[1];
>> v = b - a;
>> if (((v - 1) & v) == 0 && v > r)
>> r = v;
>>
>> return r;
>> }

all these below warning should be for the function ele();
where is the problem in access data
using unsigned char pointers?
the only problem that came in mind is if
uint8_t!=unsigned char
and uint8_t* has incompatible reppresentation
with unsigned char
what about the use of cast?

uint32_t  ele(void)
{long double  ldbt[2];
 double        dbt[2];
 size_t        szt[2];
 uint8_t       art[2], *a, *b;
 long unsigned lut[2];
 uint32_t        r, v;

 r=0;

 a=(uint8_t*)ldbt;  b=(uint8_t*)(ldbt+1); v=b-a;
 /* 100b-1b=11 */
 if(((v-1)&v)==0 && v>r)  r=v;
 a=(uint8_t*)dbt;   b=(uint8_t*)(dbt +1); v=b-a;
 if(((v-1)&v)==0 && v>r)  r=v;
 a=(uint8_t*)szt;   b=(uint8_t*)(szt +1); v=b-a;
 if(((v-1)&v)==0 && v>r)  r=v;
 a=(uint8_t*)art;   b=(uint8_t*)(art +1); v=b-a;
 if(((v-1)&v)==0 && v>r)  r=v;
 a=(uint8_t*)lut;   b=(uint8_t*)(lut +1); v=b-a;
 if(((v-1)&v)==0 && v>r)  r=v;

 return  r;
}


> GCC gave:
>
> io_x2.c: In function 'ele':
> io_x2.c:15: warning: assignment from incompatible pointer type
> io_x2.c:16: warning: assignment from incompatible pointer type
> io_x2.c:24: warning: assignment from incompatible pointer type
> io_x2.c:25: warning: assignment from incompatible pointer type
> io_x2.c:33: warning: assignment from incompatible pointer type
> io_x2.c:34: warning: assignment from incompatible pointer type
> io_x2.c:39: warning: assignment from incompatible pointer type
> io_x2.c:40: warning: assignment from incompatible pointer type
> io_x2.c:45: warning: assignment from incompatible pointer type
> io_x2.c:46: warning: assignment from incompatible pointer type




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


#6203

FromShao Miller <sha0.miller@gmail.com>
Date2011-06-16 19:05 -0500
Message-ID<ite5or$e9m$1@dont-email.me>
In reply to#6141
On 6/16/2011 3:35 AM, io_x wrote:
> "io_x"<a@b.c.invalid>  ha scritto nel messaggio
> news:4df70a29$0$2690$4fafbaef@reader1.news.tin.it...
>
> what about my try?
>
> [...code...]

I find it a bit crammed and difficult to read, so here's an adaptation 
of the code by performing preprocessor expansion, followed by an 
'indent' invocation:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

union tipidiarray {
         double a;
         size_t v;
         uint8_t *m;
};

uint32_t elemento = sizeof(union tipidiarray);

uint8_t *IniMem(uint32_t s)
{
         uint8_t *p;
         uint32_t *pu;
         uint32_t c;

         if (s == 0)
                 return 0;
         c = s;
         c += elemento;
         if (c < s)
                 return 0;
         p = malloc(c);
         if (p == 0)
                 return 0;
         pu = (uint32_t *) p;
         p += elemento;
         pu[0] = 1;
         pu[1] = (uint32_t) p;
         return p;
}

uint8_t *GetRef(uint8_t * v)
{
         uint8_t *p;
         uint32_t *pu;

         if (v == 0)
                 return 0;
         p = v - elemento;
         pu = (uint32_t *) p;
         if (pu[0] == 0 || pu[1] != (uint32_t) v)
                 return 0;
         ++pu[0];
         if (pu[0] == 0) {
                 --pu[0];
                 return 0;
         }
         return v;
}

uint32_t GetRefCounter(uint8_t * v)
{
         uint8_t *p;
         uint32_t *pu;

         if (v == 0)
                 return 0;
         p = v - elemento;
         pu = (uint32_t *) p;
         if (pu[1] != (uint32_t) v)
                 return (uint32_t) - 1;
         return pu[0];
}

uint8_t *PutRef(uint32_t * ris, uint8_t * v)
{
         uint8_t *p;
         uint32_t *pu;

         if (ris == 0 || v == 0)
                 return 0;
         *ris = 0;
         p = v - elemento;
         pu = (uint32_t *) p;
         if (pu[0] == 0 || pu[1] != (uint32_t) v) {
                 *ris = -1;
                 return 0;
         }
         --pu[0];
         if (pu[0] == 0) {
                 pu[1] = 0;
                 free(p);
         }
         return 0;
}

int main(void)
{
         double *arrd, *pad;
         uint32_t *arru, *elm, ris;

         if (2 * sizeof(uint32_t) > elemento) {
                 printf("Errore di inizializzazione\n");
                 return 0;
         }
         arrd = (double *)IniMem(10 * sizeof(double));
         arrd[0] = 1.111;
         pad = (double *)GetRef((uint8_t *) arrd);
         if (pad == 0) {
                 printf("Errore della funzione GetRef()\n");
                 printf("o memoria insufficiente\n");
                 exit(0);
         }
         printf("val=%f nref=%u\n", pad[0],
                (unsigned)GetRefCounter((uint8_t *) arrd));
         pad = (double *)PutRef(&ris, (uint8_t *) pad);
         printf("val=%f nref=%u pad=%p ris=%u\n",
                arrd[0], (unsigned)GetRefCounter((uint8_t *) arrd),
                (uint8_t *) pad, (unsigned)ris);

         arrd = (double *)PutRef(&ris, (uint8_t *) pad);
         printf("nref=%u ris=%u\n", (unsigned)GetRefCounter((uint8_t *) 
arrd),
                (unsigned)ris);
         return 0;
}

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


#6204

FromIan Collins <ian-news@hotmail.com>
Date2011-06-17 12:09 +1200
Message-ID<95vk9pFu2qU2@mid.individual.net>
In reply to#6203
On 06/17/11 12:05 PM, Shao Miller wrote:
> On 6/16/2011 3:35 AM, io_x wrote:
>> "io_x"<a@b.c.invalid>   ha scritto nel messaggio
>> news:4df70a29$0$2690$4fafbaef@reader1.news.tin.it...
>>
>> what about my try?
>>
>> [...code...]
>
> I find it a bit crammed and difficult to read, so here's an adaptation
> of the code by performing preprocessor expansion, followed by an
> 'indent' invocation:

That's one way to get rid of those stupid macros!

-- 
Ian Collins

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


#6110

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-06-15 08:40 -0700
Message-ID<daa83558-8cbd-457f-9414-7ff18b3b5a9e@28g2000yqu.googlegroups.com>
In reply to#5477
On Jun 5, 5:52 am, daknok <radekslu...@gmail.com> wrote:
> Hi,
>
> If I want to do simple reference counting for different structures I
> would go like this:
>
[deletia]
>
> Though if I have about ten structs, I have ten different functions to
> increase and decrease the reference count. Is it, in some way, possible
> to just provide one function for increasing and one function for
> decreasing the reference count which can be used for all structs with a
> reference count?


+1 for an excellent question leading to a very interesting thread!

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


#6174

From"christian.bau" <christian.bau@cbau.wanadoo.co.uk>
Date2011-06-16 11:08 -0700
Message-ID<896e139b-15e1-4f35-aa97-cae9b46f4ea4@h12g2000pro.googlegroups.com>
In reply to#5477
On Jun 5, 11:52 am, daknok <radekslu...@gmail.com> wrote:

> Though if I have about ten structs, I have ten different functions to
> increase and decrease the reference count. Is it, in some way, possible
> to just provide one function for increasing and one function for
> decreasing the reference count which can be used for all structs with a
> reference count?

Go to the apple website, look around for the Core Foundation source
code, and look at the source code for CFRetain / CFRelease.

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


#6176

FromShao Miller <sha0.miller@gmail.com>
Date2011-06-16 14:42 -0400
Message-ID<itdikt$5ud$1@dont-email.me>
In reply to#6174
On 6/16/2011 14:08, christian.bau wrote:
> On Jun 5, 11:52 am, daknok<radekslu...@gmail.com>  wrote:
>
>> Though if I have about ten structs, I have ten different functions to
>> increase and decrease the reference count. Is it, in some way, possible
>> to just provide one function for increasing and one function for
>> decreasing the reference count which can be used for all structs with a
>> reference count?
>
> Go to the apple website, look around for the Core Foundation source
> code, and look at the source code for CFRetain / CFRelease.

To save people some time, here it is (I think):

   http://www.opensource.apple.com/source/CF/CF-550.42/CFRuntime.c

[toc] | [prev] | [standalone]


Page 4 of 4 — ← Prev page 1 2 3 [4]

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


csiph-web