123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146(**************************************************************************)(* Lablgtk *)(* *)(* This program is free software; you can redistribute it *)(* and/or modify it under the terms of the GNU Library General *)(* Public License as published by the Free Software Foundation *)(* version 2, with the exception described in file COPYING which *)(* comes with the library. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Library General Public License for more details. *)(* *)(* You should have received a copy of the GNU Library General *)(* Public License along with this program; if not, write to the *)(* Free Software Foundation, Inc., 59 Temple Place, Suite 330, *)(* Boston, MA 02111-1307 USA *)(* *)(* *)(**************************************************************************)(* $Id$ *)openStdLabels(* marked pointers *)type'aoptaddrletoptaddr:'aoption->'aoptaddr=functionNone->Obj.magic0|Somex->Obj.magicx(* boxed pointers *)typeboxedletboxed_null:boxed=Obj.magicNativeint.zeroexternalpeek_string:?pos:int->?len:int->boxed->string="ml_string_at_pointer"externalpeek_int:boxed->int="ml_int_at_pointer"externalpoke_int:boxed->int->unit="ml_set_int_at_pointer"externalpeek_nativeint:boxed->nativeint="ml_long_at_pointer"externalpoke_nativeint:boxed->nativeint->unit="ml_set_long_at_pointer"type'aoptboxedletoptboxed:'aoption->'aoptboxed=functionNone->Obj.magicboxed_null|Someobj->Obj.magicobjletmay_box~fobj:'aoptboxed=matchobjwithNone->Obj.magicboxed_null|Someobj->Obj.magic(fobj:'a)(* Variant tables *)type'avariant_tableconstraint'a=[>]externaldecode_variant:'avariant_table->int->'a="ml_ml_lookup_from_c"externalencode_variant:'avariant_table->'a->int="ml_ml_lookup_to_c"letencode_flagstbll=List.fold_leftl~init:0~f:(funaccv->acclor(encode_varianttblv))letdecode_flagstblc=letl=ref[]infori=30downto0do(* only 31-bits in ocaml usual integers *)letd=1lsliinifclandd<>0thenl:=decode_varianttbld::!ldone;!l(* Exceptions *)exceptionNulllet_=Callback.register_exception"null_pointer"Null(* Stable pointer *)type'astableexternalstable_copy:'a->'astable="ml_stable_copy"(* Region pointers *)typeregion={data:Obj.t;path:intarray;offset:int;length:int}letlengthreg=reg.lengthletunsafe_create_region~path~get_lengthdata={data=Obj.reprdata;path=path;offset=0;length=get_lengthdata}letsub?(pos=0)?lenreg=letlen=matchlenwithSomex->x|None->reg.length-posinifpos<0||pos>reg.length||pos+len>reg.lengththeninvalid_arg"Gpointer.sub";{regwithoffset=reg.offset+pos;length=len}externalunsafe_get_byte:region->pos:int->int="ml_gpointer_get_char"externalunsafe_set_byte:region->pos:int->int->unit="ml_gpointer_set_char"externalunsafe_blit:src:region->dst:region->unit="ml_gpointer_blit"(* handle with care, if allocation not static *)externalget_addr:region->nativeint="ml_gpointer_get_addr"letget_bytereg~pos=ifpos>=reg.lengththeninvalid_arg"Gpointer.get_char";unsafe_get_bytereg~posletset_bytereg~posch=ifpos>=reg.lengththeninvalid_arg"Gpointer.set_char";unsafe_set_bytereg~poschletblit~src~dst=ifsrc.length<>dst.lengththeninvalid_arg"Gpointer.blit";unsafe_blit~src~dst(* Making a region from a string is easy *)letregion_of_bytes=unsafe_create_region~path:[||]~get_length:Bytes.lengthletbytes_of_regionreg=lets=Bytes.createreg.lengthinletreg'=region_of_bytessinunsafe_blitregreg';s(* Access bigarrays breaking the abstraction... dirty *)type'abigarray=(int,Bigarray.int8_unsigned_elt,'a)Bigarray.Array1.tletbigarray_size(arr:'abigarray)=letsize={data=Obj.reprarr;path=[|1+4|];offset=0;length=0}inNativeint.to_int(get_addrsize)letregion_of_bigarrayarr=unsafe_create_region~path:[|1|]~get_length:bigarray_sizearr