caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] help with caml_stat_alloc
@ 2006-07-24  7:50 Jonathan Roewen
  2006-07-24  8:56 ` Richard Jones
  2006-07-24 11:39 ` Anatoly Zaretsky
  0 siblings, 2 replies; 4+ messages in thread
From: Jonathan Roewen @ 2006-07-24  7:50 UTC (permalink / raw)
  To: caml-list

Hi,

I'm trying to allocate some memory outside of the caml heap, and I 
think caml_stat_alloc is the right function to use. Anyways, it's 
generating seg-faults, so I'm hoping someone can help me fix my 
implementation.

CAMLprim value icfp_alloc(value size) {
	int sz = Int32_val(size) + 1;
	int *p = (int *)caml_stat_alloc(sz);
	p[--sz] = sz;
	/*while ( sz > 0 ) {
		p[--sz] = 0;
	}*/
	return caml_copy_int32(p);
}

Also, my interface in ocaml is:

external alloc : int32 -> int32 = "icfp_alloc"
external free : int32 -> unit = "icfp_free"
external get : int32 -> int32 -> int32 = "icfp_get"
external set : int32 -> int32 -> int32 -> unit = "icfp_set"
external copy : int32 -> int32 = "icfp_copy"

Some guidance would be much appreciated,

Jonathan


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] help with caml_stat_alloc
  2006-07-24  7:50 [Caml-list] help with caml_stat_alloc Jonathan Roewen
@ 2006-07-24  8:56 ` Richard Jones
  2006-07-24 10:13   ` Olivier Andrieu
  2006-07-24 11:39 ` Anatoly Zaretsky
  1 sibling, 1 reply; 4+ messages in thread
From: Richard Jones @ 2006-07-24  8:56 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

On Mon, Jul 24, 2006 at 12:50:32AM -0700, Jonathan Roewen wrote:
> I'm trying to allocate some memory outside of the caml heap, and I 
> think caml_stat_alloc is the right function to use. Anyways, it's 
> generating seg-faults, so I'm hoping someone can help me fix my 
> implementation.

What was wrong with malloc?

Have a look at ocamlode (http://merjis.com/developers/ocamlode), file
ode_c.c for some functions which wrap external pointers in custom
blocks.

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] help with caml_stat_alloc
  2006-07-24  8:56 ` Richard Jones
@ 2006-07-24 10:13   ` Olivier Andrieu
  0 siblings, 0 replies; 4+ messages in thread
From: Olivier Andrieu @ 2006-07-24 10:13 UTC (permalink / raw)
  To: Richard Jones; +Cc: Jonathan Roewen, caml-list

 Richard Jones [Monday 24 July 2006] :
 >
 > On Mon, Jul 24, 2006 at 12:50:32AM -0700, Jonathan Roewen wrote:
 > > I'm trying to allocate some memory outside of the caml heap, and
 > > I think caml_stat_alloc is the right function to use. Anyways,
 > > it's generating seg-faults, so I'm hoping someone can help me fix
 > > my implementation.
 > 
 > What was wrong with malloc?

caml_stat_alloc _is_ malloc, it's just that it raises the ocaml
out-of-memory exception if malloc returns NULL.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] help with caml_stat_alloc
  2006-07-24  7:50 [Caml-list] help with caml_stat_alloc Jonathan Roewen
  2006-07-24  8:56 ` Richard Jones
@ 2006-07-24 11:39 ` Anatoly Zaretsky
  1 sibling, 0 replies; 4+ messages in thread
From: Anatoly Zaretsky @ 2006-07-24 11:39 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

On 7/24/06, Jonathan Roewen <jonathan.roewen@gmail.com> wrote:
> Hi,
>
> I'm trying to allocate some memory outside of the caml heap, and I
> think caml_stat_alloc is the right function to use. Anyways, it's
> generating seg-faults, so I'm hoping someone can help me fix my
> implementation.
>
> CAMLprim value icfp_alloc(value size) {
>         int sz = Int32_val(size) + 1;
>         int *p = (int *)caml_stat_alloc(sz);
>         p[--sz] = sz;
>         /*while ( sz > 0 ) {
>                 p[--sz] = 0;
>         }*/
>         return caml_copy_int32(p);
> }

>From byterun/memory.h:
CAMLextern void * caml_stat_alloc (asize_t);              /* Size in bytes. */

So p[--sz] is out of allocated region.

--
Tolik


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-07-24 11:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-24  7:50 [Caml-list] help with caml_stat_alloc Jonathan Roewen
2006-07-24  8:56 ` Richard Jones
2006-07-24 10:13   ` Olivier Andrieu
2006-07-24 11:39 ` Anatoly Zaretsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).