hello,
I want to call the OpenGL ES API now and I am studying the memory
allocation and pointer manipulation with ATS.
As a first test, I have made a simple C lib wich take an unsigned int
buffer and print its content.
my library code :
#include “my_lib.h”
#include <stdio.h>
void display_numbers(unsigned int* array, unsigned int size) {
int i;
if( array != NULL ) {
for(i=0;i<size;i++) {
printf("%i\n", array[i]);
}
}
}
I want to make a test code in ATS that create a buffer and set some
arbitrary values and call my library.
The equivalent C code I want to write in ATS :
#include “my_lib.h”
#include <stdlib.h>
#define LEN 17
int main(int argc, char* argv[]) {
int i;
unsigned int* my_array = (unsigned int*)malloc(LEN*sizeof(unsigned int));
for(i=0;i<LEN;i++) {
my_array[i] = LEN - i;
}
display_numbers(my_array, LEN);//–> from my external lib
free(my_array);
}
First of all, I want to find out how to make this in ATS with malloc_gc
(with pointer conversion and assignement of a pointer)
and then how to create a buffer with array_ptr_alloc (with pointer
assignment and also array [] operator )
I look this
page http://bluishcoder.co.nz/2013/01/25/an-introduction-to-pointers-in-ats.html
but I found that he didn’t use the malloc_gc function nor the array_v
dataview
I tried a lot of different things but did not find how to make it.
I dont call the display function from C library now, just allocate,
initialize and deallocate buffer
Here is a rough draft of what I would like to achieve and some help would
be appreciated
//staload "my_lib.sats"
staload "prelude/DATS/array.dats"
staload “prelude/DATS/pointer.dats”
(*
fn{a:t@ype} init_ptr_buffer{l:agz}{n:int}{m:nat} (pf: ![uint?][m] @ l|p:ptr l
, sz: size_t m) = …
*)
fn{a:t@ype} test_malloc{n:nat} (x: a, sz: size_t n): void = let
val (gc, pf|p) = malloc_gc(sz) // I want to alloc sz*sizeof(uint) bytes
// val () = init_ptr_buffer(pf|p, sz) // I need a conversion from byte
pointer to unsigned int pointer
// val () = subsequent call to display_numbers in my C library…
in
free_gc(gc, pf|p)
end
(*
fn{a:t@ype} init_array_buffer{l:agz}{n:int}{m:nat} (pf: !array_v(a, n, l)|p:ptr l
, sz: size_t m) = let
fun loop (i: int): void =
if (i = 0) then print(i)
else let val () = printf("%i\n", @(i)) in loop(i - 1) end
in
loop(p, sz) // do a countdown along the buffer
end
*)
fn{a:t@ype} test_ptr_alloc{n:nat} (x: a, sz: size_t n): void = let
val (gc, pf | p) = array_ptr_alloc(10)
// val () = init_array_buffer(pf|p, p)
// val () = subsequent call to display_numbers in my C library…
in
array_ptr_free(gc, pf|p)
end
implement main(argc, argv) = let
val () = test_malloc(uint_of 10, size1_of_int1 10)
val () = test_ptr_alloc(uint_of 10, size1_of_int1 10)
in
end
thank you for your help