www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - owner pointers

reply Sclytrack <idiot hotmail.com> writes:
Assume: no garbage collector
----------------------------

Let us say that we have two types of pointers. Owner pointers and view pointers.

owner(pointer(int))
view(pointer(int))


owner(pointer(int)) routineA()
{
  owner(pointer(int)) a = malloc();

  //The compiler checks that ownership is returned or passed on to
  //another routine. (Possible?)

  return a;
}

--------------------  //Yes 20 long "-"


void doStuffA( view(pointer(int)) param)
{
  //not in charge of the deallocation.
}

void doStuffB( owner(pointer(int)) param)
{
  free(param);
}

void routineB()
{
  owner(pointer(int)) a = malloc();
  doStuffA(a);
  doStuffB(a);  //Passes on ownership.
  //routineB is no longer in charge of the deallocation, because of doStuffB.
}

--------------------

void usingStuff()
{
  owner(pointer(int)) a = routineA();
  //You are in charge of the deallocation.
  free(a);
}

--------------------
Nov 29 2009
parent "Denis Koroskin" <2korden gmail.com> writes:
On Sun, 29 Nov 2009 13:10:27 +0300, Sclytrack <idiot hotmail.com> wrote:

 Assume: no garbage collector
 ----------------------------

 Let us say that we have two types of pointers. Owner pointers and view  
 pointers.

 owner(pointer(int))
 view(pointer(int))


 owner(pointer(int)) routineA()
 {
   owner(pointer(int)) a = malloc();

   //The compiler checks that ownership is returned or passed on to
   //another routine. (Possible?)

   return a;
 }

 --------------------  //Yes 20 long "-"


 void doStuffA( view(pointer(int)) param)
 {
   //not in charge of the deallocation.
 }

 void doStuffB( owner(pointer(int)) param)
 {
   free(param);
 }

 void routineB()
 {
   owner(pointer(int)) a = malloc();
   doStuffA(a);
   doStuffB(a);  //Passes on ownership.
   //routineB is no longer in charge of the deallocation, because of  
 doStuffB.
 }

 --------------------

 void usingStuff()
 {
   owner(pointer(int)) a = routineA();
   //You are in charge of the deallocation.
   free(a);
 }

 --------------------
What's the point of this post?
Nov 29 2009