Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Ru/IO Inside
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Example: a memory allocator generator === As an example, one of my programs has a module which is a memory suballocator. It receives the address and size of a large memory block and returns two procedures - one to allocate a subblock of a given size and the other to free the allocated subblock: <haskell> memoryAllocator :: Ptr a -> Int -> IO (Int -> IO (Ptr b), Ptr c -> IO ()) memoryAllocator buf size = do ...... let alloc size = do ... ... free ptr = do ... ... return (alloc, free) </haskell> How this is implemented? 'alloc' and 'free' work with references created inside the memoryAllocator procedure. Because the creation of these references is a part of the memoryAllocator IO actions chain, a new independent set of references will be created for each memory block for which memoryAllocator is called: <haskell> memoryAllocator buf size = do start <- newIORef buf end <- newIORef (buf `plusPtr` size) ... </haskell> These two references are read and written in the 'alloc' and 'free' definitions (we'll implement a very simple memory allocator for this example): <haskell> ... let alloc size = do addr <- readIORef start writeIORef start (addr `plusPtr` size) return addr let free ptr = do writeIORef start ptr </haskell> What we've defined here is just a pair of closures that use state available at the moment of their definition. As you can see, it's as easy as in any other functional language, despite Haskell's lack of direct support for impure functions. The following example uses procedures, returned by memoryAllocator, to simultaneously allocate/free blocks in two independent memory buffers: <haskell> main = do buf1 <- mallocBytes (2^16) buf2 <- mallocBytes (2^20) (alloc1, free1) <- memoryAllocator buf1 (2^16) (alloc2, free2) <- memoryAllocator buf2 (2^20) ptr11 <- alloc1 100 ptr21 <- alloc2 1000 free1 ptr11 free2 ptr21 ptr12 <- alloc1 100 ptr22 <- alloc2 1000 </haskell>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width