Allocating Memory

LayGO for Python 1.5 contains 4 functions which make it easy to allocate memory suitable for passing to the C language functions of the LayGO API.

maxBuffer  = lgo_MaxBufferSize(cid)
inBuffer   = lgo_BufferNew(maxBuffer)
outBuffer  = lgo_BufferNew(maxBuffer)

for i in range(maxBuffer):
    lgo_BufferSet(outBuffer, i, 65 + (i % 26))

result = lgo_Write(cid, outBuffer, maxBuffer)

if (result < 0):
    print "Error writing:", lgo_ErrorMessage(result)
else:
    print `result`, "bytes written"

    for i in range(0, result, 16):
        for j in range(i, min(result, i + 16)):
            print " %02X" % (lgo_BufferGet(outBuffer, j)),
        print ""

    result = 0

    while (result <= 0):
        time.sleep(1.0)
        result = lgo_Read(cid, inBuffer, maxBuffer)

    print `result`,  "bytes received."

    for i in range(0, result, 16):
        for j in range(i, min(result, i + 16)):
            print " %02X" % (lgo_BufferGet(inBuffer, j)),
        print ""

lgo_BufferFree(inBuffer)
lgo_BufferFree(outBuffer)