Skip to content

Commit cf2b05c

Browse files
madman-bobgallais
authored andcommitted
Add RefC Buffer support
1 parent ecde887 commit cf2b05c

File tree

9 files changed

+80
-2
lines changed

9 files changed

+80
-2
lines changed

libs/base/Data/Buffer.idr

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export
1616
data Buffer : Type where [external]
1717

1818
%foreign "scheme:blodwen-buffer-size"
19+
"C:idris2_getBufferSize, libidris2_support, idris_buffer.h"
1920
"node:lambda:b => BigInt(b.length)"
2021
prim__bufferSize : Buffer -> Int
2122

@@ -24,6 +25,7 @@ rawSize : HasIO io => Buffer -> io Int
2425
rawSize buf = pure (prim__bufferSize buf)
2526

2627
%foreign "scheme:blodwen-new-buffer"
28+
"C:idris2_newBuffer, libidris2_support, idris_buffer.h"
2729
"node:lambda:s=>Buffer.alloc(Number(s))"
2830
prim__newBuffer : Int -> PrimIO Buffer
2931

@@ -36,16 +38,22 @@ newBuffer size
3638
-- then pure Nothing
3739
-- else pure $ Just $ MkBuffer buf size 0
3840

39-
-- might be needed if we do this in C...
41+
%foreign "scheme:blodwen-buffer-free"
42+
"C:idris2_freeBuffer, libidris2_support, idris_buffer.h"
43+
"node:lambda:buf=>undefined"
44+
prim__freeBuffer : Buffer -> PrimIO ()
45+
4046
export
4147
freeBuffer : HasIO io => Buffer -> io ()
42-
freeBuffer buf = pure ()
48+
freeBuffer buf = primIO (prim__freeBuffer buf)
4349

4450
%foreign "scheme:blodwen-buffer-setbyte"
51+
"C:idris2_setBufferByte, libidris2_support, idris_buffer.h"
4552
"node:lambda:(buf,offset,value)=>buf.writeUInt8(Number(value), Number(offset))"
4653
prim__setByte : Buffer -> Int -> Int -> PrimIO ()
4754

4855
%foreign "scheme:blodwen-buffer-setbyte"
56+
"C:idris2_setBufferByte, libidris2_support, idris_buffer.h"
4957
"node:lambda:(buf,offset,value)=>buf.writeUInt8(Number(value), Number(offset))"
5058
prim__setBits8 : Buffer -> Int -> Bits8 -> PrimIO ()
5159

@@ -61,10 +69,12 @@ setBits8 buf loc val
6169
= primIO (prim__setBits8 buf loc val)
6270

6371
%foreign "scheme:blodwen-buffer-getbyte"
72+
"C:idris2_getBufferByte, libidris2_support, idris_buffer.h"
6473
"node:lambda:(buf,offset)=>BigInt(buf.readUInt8(Number(offset)))"
6574
prim__getByte : Buffer -> Int -> PrimIO Int
6675

6776
%foreign "scheme:blodwen-buffer-getbyte"
77+
"C:idris2_getBufferByte, libidris2_support, idris_buffer.h"
6878
"node:lambda:(buf,offset)=>BigInt(buf.readUInt8(Number(offset)))"
6979
prim__getBits8 : Buffer -> Int -> PrimIO Bits8
7080

@@ -149,6 +159,7 @@ getInt32 buf loc
149159
= primIO (prim__getInt32 buf loc)
150160

151161
%foreign "scheme:blodwen-buffer-setint"
162+
"C:idris2_setBufferInt, libidris2_support, idris_buffer.h"
152163
"node:lambda:(buf,offset,value)=>buf.writeInt64(Number(value), Number(offset))"
153164
prim__setInt : Buffer -> Int -> Int -> PrimIO ()
154165

@@ -158,6 +169,7 @@ setInt buf loc val
158169
= primIO (prim__setInt buf loc val)
159170

160171
%foreign "scheme:blodwen-buffer-getint"
172+
"C:idris2_getBufferInt, libidris2_support, idris_buffer.h"
161173
"node:lambda:(buf,offset)=>BigInt(buf.readInt64(Number(offset)))"
162174
prim__getInt : Buffer -> Int -> PrimIO Int
163175

@@ -167,6 +179,7 @@ getInt buf loc
167179
= primIO (prim__getInt buf loc)
168180

169181
%foreign "scheme:blodwen-buffer-setdouble"
182+
"C:idris2_setBufferDouble, libidris2_support, idris_buffer.h"
170183
"node:lambda:(buf,offset,value)=>buf.writeDoubleLE(value, Number(offset))"
171184
prim__setDouble : Buffer -> Int -> Double -> PrimIO ()
172185

@@ -176,6 +189,7 @@ setDouble buf loc val
176189
= primIO (prim__setDouble buf loc val)
177190

178191
%foreign "scheme:blodwen-buffer-getdouble"
192+
"C:idris2_getBufferDouble, libidris2_support, idris_buffer.h"
179193
"node:lambda:(buf,offset)=>buf.readDoubleLE(Number(offset))"
180194
prim__getDouble : Buffer -> Int -> PrimIO Double
181195

@@ -190,6 +204,7 @@ export
190204
stringByteLength : String -> Int
191205

192206
%foreign "scheme:blodwen-buffer-setstring"
207+
"C:idris2_setBufferString, libidris2_support, idris_buffer.h"
193208
"node:lambda:(buf,offset,value)=>buf.write(value, Number(offset),buf.length - Number(offset), 'utf-8')"
194209
prim__setString : Buffer -> Int -> String -> PrimIO ()
195210

@@ -199,6 +214,7 @@ setString buf loc val
199214
= primIO (prim__setString buf loc val)
200215

201216
%foreign "scheme:blodwen-buffer-getstring"
217+
"C:idris2_getBufferString, libidris2_support, idris_buffer.h"
202218
"node:lambda:(buf,offset,len)=>buf.slice(Number(offset), Number(offset+len)).toString('utf-8')"
203219
prim__getString : Buffer -> Int -> Int -> PrimIO String
204220

@@ -221,6 +237,7 @@ bufferData buf
221237

222238

223239
%foreign "scheme:blodwen-buffer-copydata"
240+
"C:idris2_copyBuffer, libidris2_support, idris_buffer.h"
224241
"node:lambda:(b1,o1,length,b2,o2)=>b1.copy(b2,Number(o2), Number(o1), Number(o1+length))"
225242
prim__copyData : Buffer -> Int -> Int -> Buffer -> Int -> PrimIO ()
226243

support/chez/support.ss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@
186186
(define (blodwen-buffer-size buf)
187187
(bytevector-length buf))
188188

189+
(define (blodwen-buffer-free buf)
190+
(void)) ; Rely on built-in memory management
191+
189192
(define (blodwen-buffer-setbyte buf loc val)
190193
(bytevector-u8-set! buf loc val))
191194

support/racket/support.rkt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@
179179
(define (blodwen-buffer-size buf)
180180
(bytevector-length buf))
181181

182+
(define (blodwen-buffer-free buf)
183+
(void)) ; Rely on built-in memory management
184+
182185
(define (blodwen-buffer-setbyte buf loc val)
183186
(bytevector-u8-set! buf loc val))
184187

support/refc/memoryManagement.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ Value_GCPointer *makeGCPointer(void *ptr_Raw, Value_Closure *onCollectFct)
131131
return p;
132132
}
133133

134+
Value_Buffer *makeBuffer(void *buf)
135+
{
136+
Value_Buffer *b = (Value_Buffer *)newValue();
137+
b->header.tag = BUFFER_TAG;
138+
b->buffer = buf;
139+
return b;
140+
}
141+
134142
Value_Array *makeArray(int length)
135143
{
136144
Value_Array *a = (Value_Array *)newValue();

support/refc/memoryManagement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Value_String *makeString(char *);
2323

2424
Value_Pointer *makePointer(void *);
2525
Value_GCPointer *makeGCPointer(void *ptr_Raw, Value_Closure *onCollectFct);
26+
Value_Buffer *makeBuffer(void *buf);
2627
Value_Array *makeArray(int length);
2728
Value_World *makeWorld(void);
2829

tests/Main.idr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ refcTests : TestPool
224224
refcTests = MkTestPool "Reference counting C backend" [C]
225225
[ "refc001" , "refc002"
226226
, "strings", "doubles"
227+
, "buffer"
227228
]
228229

229230
racketTests : TestPool

tests/refc/buffer/TestBuffer.idr

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module TestBuffer
2+
3+
import Data.Buffer
4+
5+
put : Show a => IO a -> IO ()
6+
put = (>>= putStrLn . show)
7+
8+
main : IO ()
9+
main = do
10+
Just buf <- newBuffer 23
11+
| Nothing => pure ()
12+
13+
setByte buf 0 1
14+
setBits8 buf 1 2
15+
setDouble buf 2 (sqrt 2)
16+
17+
let helloWorld = "Hello, world"
18+
19+
Just helloWorldBuf <- newBuffer (stringByteLength helloWorld)
20+
| Nothing => pure ()
21+
22+
setString helloWorldBuf 0 "Hello, world"
23+
copyData helloWorldBuf 0 12 buf 10
24+
25+
put $ rawSize buf
26+
27+
put $ getByte buf 0
28+
put $ getBits8 buf 1
29+
put $ getDouble buf 2
30+
put $ getString buf 10 12
31+
32+
put $ bufferData buf
33+
34+
freeBuffer helloWorldBuf
35+
freeBuffer buf

tests/refc/buffer/expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
23
2+
1
3+
2
4+
1.414214
5+
"Hello, world"
6+
[1, 2, 205, 59, 127, 102, 158, 160, 246, 63, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 0]

tests/refc/buffer/run

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$1 --no-banner --no-color --console-width 0 --cg refc -o refc_buffer TestBuffer.idr > /dev/null
2+
./build/exec/refc_buffer
3+
4+
rm -rf build

0 commit comments

Comments
 (0)