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


Groups > comp.lang.c > #6203

Re: Reference counting

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: Reference counting
Date 2011-06-16 19:05 -0500
Organization A noiseless patient Spider
Message-ID <ite5or$e9m$1@dont-email.me> (permalink)
References <4deb6008$0$28457$703f8584@textnews.kpn.nl> <4df6ff99$0$2687$4fafbaef@reader1.news.tin.it> <4df70a29$0$2690$4fafbaef@reader1.news.tin.it> <4df9c004$0$40246$4fafbaef@reader1.news.tin.it>

Show all headers | View raw


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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web