Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8054577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:10:50+00:00 2026-06-05T08:10:50+00:00

In particular, I’m trying to learn Haskell’s foreign function interface by writing a binding

  • 0

In particular, I’m trying to learn Haskell’s foreign function interface by writing a binding to the Allegro game library. In Allegro’s native C, the main event loop looks something like this:

// initialize event struct
ALLEGRO_EVENT event;

// main loop
while (running) {
    if (al_get_next_event(event_queue, &event)) {
        // process event here ...
    }
}

Using ghc and hsc2hs, I can write a foreign function call like:

foreign import ccall "allegro5/allegro.h al_get_next_event"
    alGetNextEvent :: EventQueue -> Ptr (Event) -> IO (CInt)

where EventQueue is a pointer to an opaque structure and Event is a Storable instance based off of C’s ALLEGRO_EVENT.

Ideally, for the user-facing Haskell function, I would like to have a type signature like

getNextEvent :: EventQueue -> Maybe Event

which would abstract away initializing the ALLEGRO_EVENT struct and boolean return value.

My question is, how should I write this function to maximize memory efficiency? I could malloc a new pointer to Event inside the method and use that, but since I’m working with C-based data, I want to make sure I’m re-using existing space and not constantly allocating new structs. I also want to avoid having the user malloc the struct and pass it in to every call.

Any advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:10:51+00:00Added an answer on June 5, 2026 at 8:10 am

    Typically, if it is locally scoped data, you would use

    • alloca

    If the data has longer scope,

    • mallocForeignPtr

    is a good choice, since it is very fast, you can attach finalizers, and you don’t have to clean up on your own.

    The GHC runtime takes care of maximally reusing space for you, so there’s not too much of a need to worry about efficiency by e.g. pinning a mutable memory buffer in place. Just let the GC take care of things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This particular assignment has to do with removing substrings from strings; I am trying
In particular Create a function to take an array and an index as parameters.
Are there particular cases where native text manipulation is more desirable than regex? In
I want to find particular string in JAVA and I am using CONTAINS function
This particular question is more about trying to see the reason behind compiler message
any particular function or code to put this kind of array data ori [0]
The particular situation I am currently trying to get around involves a bool? referencing
I have a particular function (well, set of functions) that I want to start
In particular, can filtered replication be done with PouchDB? I want to make sure
My particular scenario involves doing some text transformation using regular expressions within a private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.