+-- | Allocate a block of memory that is sufficient to hold values of type
+-- @a@. The size of the area allocated is determined by the 'sizeOf'
+-- method from the instance of 'Storable' for the appropriate type.
+-- The memory is initalized to 0.
+--
+-- The memory may be deallocated using 'free' or 'finalizerFree' when
+-- no longer required.
+--
+{-# INLINE calloc #-}
+calloc :: Storable a => IO (Ptr a)
+calloc = doCalloc undefined
+ where
+ doCalloc :: Storable b => b -> IO (Ptr b)
+ doCalloc dummy = callocBytes (sizeOf dummy)
+
+-- | Allocate a block of memory of the given number of bytes.
+-- The block of memory is sufficiently aligned for any of the basic
+-- foreign tyes that fit into a memory block of the allocated size.
+-- The memory is initialized to 0.
+--
+-- The memory may be deallocated using 'free' or 'finalizerFree' when
+-- no longer required.
+--
+callocBytes :: Int -> IO (Ptr a)
+callocBytes size = failWhenNULL "calloc" (_calloc (fromIntegral size) 1)
+
-- |Resize a memory area that was allocated with 'malloc' or 'mallocBytes'
-- to the size needed to store values of type @b@. The returned pointer
-- may refer to an entirely different memory area, but will be suitably
--
1.7.4.1

+-- | Allocate a block of memory that is sufficient to hold values of type
+-- @a@. The size of the area allocated is determined by the 'sizeOf'
+-- method from the instance of 'Storable' for the appropriate type.
+-- The memory is initalized to 0.
+--
+-- The memory may be deallocated using 'free' or 'finalizerFree' when
+-- no longer required.
+--
+{-# INLINE calloc #-}
+calloc :: Storable a => IO (Ptr a)
+calloc = doCalloc undefined
+ where
+ doCalloc :: Storable b => b -> IO (Ptr b)
+ doCalloc dummy = callocBytes (sizeOf dummy)
+
+-- | Allocate a block of memory of the given number of bytes.
+-- The block of memory is sufficiently aligned for any of the basic
+-- foreign tyes that fit into a memory block of the allocated size.
+-- The memory is initialized to 0.
+--
+-- The memory may be deallocated using 'free' or 'finalizerFree' when
+-- no longer required.
+--
+callocBytes :: Int -> IO (Ptr a)
+callocBytes size = failWhenNULL "calloc" (_calloc (fromIntegral size) 1)
+
-- |Resize a memory area that was allocated with 'malloc' or 'mallocBytes'
-- to the size needed to store values of type @b@. The returned pointer
-- may refer to an entirely different memory area, but will be suitably--
1.7.4.1

Thanks. I admit I did not read that before sending these patches but now I need further clarification.

It says to send a patch and I did that. I made the changes against the latest version of base (well, it was the latest version when I sent the patch almost 3 weeks ago). It's very hard for me to test portability. I don't have hugs on any machines. The most I could do there is test on a few OSs with ghc. I've done my best to follow the style and create documentation. As for testing, I put them on hackage where I've used them in at least one package successfully.

My rationale is simple. Sometimes you need calloc instead of malloc and there was no existing binding so I created one. It seems like it should live in base.

That wiki page doesn't say how long the discussion period should be or give advice on how to determine the size. What would you recommend for this patch?

Re: [PATCH 1/2] add calloc and callocBytes to Foreign.Marshal.Alloc

On Fri, Apr 22, 2011 at 07:20:55AM -0700, Jason Dagit wrote:
>
> That wiki page doesn't say how long the discussion period should be or give
> advice on how to determine the size. What would you recommend for this
> patch?

I just updated the page to recommend 2 weeks, which I think is the
status quo.

> So it sounds like the things I missed were:
> * putting "Proposal:" in the subject line
> * setting a discussion period
> * attaching my patch to the trac instance (can I send a pull request on
> github instead?)

You don't need to make a trac ticket for it until the proposal has been
accepted.

Anything that means we see that the patch needs to be applied is OK.
Currently pull requests don't get sent to the mailing list though, and
there doesn't seem to be an easy way to set that up.

I also didn't notice a rationale for the change. The type signatures
looked the same as for the malloc functions, so I don't know what the
difference is.

Re: [PATCH 1/2] add calloc and callocBytes to Foreign.Marshal.Alloc

On Fri, Apr 22, 2011 at 07:20:55AM -0700, Jason Dagit wrote:
>
> That wiki page doesn't say how long the discussion period should be or give
> advice on how to determine the size. What would you recommend for this
> patch?

I just updated the page to recommend 2 weeks, which I think is the
status quo.

> So it sounds like the things I missed were:
> * putting "Proposal:" in the subject line
> * setting a discussion period
> * attaching my patch to the trac instance (can I send a pull request on
> github instead?)

You don't need to make a trac ticket for it until the proposal has been
accepted.

Right, I did understand that.

Anything that means we see that the patch needs to be applied is OK.
Currently pull requests don't get sent to the mailing list though, and
there doesn't seem to be an easy way to set that up.

Good to know.

I also didn't notice a rationale for the change. The type signatures
looked the same as for the malloc functions, so I don't know what the
difference is.

The difference is that calloc will initialize the allocated memory to zeros. Some C libraries assume this, at least freetype2 does, and since I want to use that library from Haskell I needed a binding to calloc. I assume others will run into this need eventually as well. When I send a "proposal:..." email, hopefully this weekend, I'll definitely make the rationale clear.

On Fri, Apr 22, 2011 at 07:20:55AM -0700, Jason Dagit wrote:
>
> That wiki page doesn't say how long the discussion period should be or give
> advice on how to determine the size. What would you recommend for this
> patch?

I just updated the page to recommend 2 weeks, which I think is the
status quo.

> So it sounds like the things I missed were:
> * putting "Proposal:" in the subject line
> * setting a discussion period
> * attaching my patch to the trac instance (can I send a pull request on
> github instead?)

You don't need to make a trac ticket for it until the proposal has been
accepted.

Right, I did understand that.

Anything that means we see that the patch needs to be applied is OK.
Currently pull requests don't get sent to the mailing list though, and
there doesn't seem to be an easy way to set that up.

Good to know.

I also didn't notice a rationale for the change. The type signatures
looked the same as for the malloc functions, so I don't know what the
difference is.

The difference is that calloc will initialize the allocated memory to zeros. Some C libraries assume this, at least freetype2 does, and since I want to use that library from Haskell I needed a binding to calloc. I assume others will run into this need eventually as well. When I send a "proposal:..." email, hopefully this weekend, I'll definitely make the rationale clear.

Re: [PATCH 1/2] add calloc and callocBytes to Foreign.Marshal.Alloc

calloc is just a multiplication followed by malloc followed by memset. Is it really worth creating a new binding for that? It always seemed like a bit of a silly API to me to begin with.

It doesn't have to be a binding. If you'd prefer to provide a Haskell implementation in terms of malloc/copyBytes that sounds fine too. I wasn't sure how to best do that so a binding seemed the most straightforward.

Note that in my binding, I gave it the same type as malloc, which is to say it is always in terms of bytes. The actual calloc api seems more like what we call mallocArray and I didn't personally need that functionality so I went for something that matched malloc. I'm flexible on that point.

On Fri, Apr 22, 2011 at 07:20:55AM -0700, Jason Dagit wrote:
>
> That wiki page doesn't say how long the discussion period should be or give
> advice on how to determine the size. What would you recommend for this
> patch?

I just updated the page to recommend 2 weeks, which I think is the
status quo.

> So it sounds like the things I missed were:
> * putting "Proposal:" in the subject line
> * setting a discussion period
> * attaching my patch to the trac instance (can I send a pull request on
> github instead?)

You don't need to make a trac ticket for it until the proposal has been
accepted.

Right, I did understand that.

Anything that means we see that the patch needs to be applied is OK.
Currently pull requests don't get sent to the mailing list though, and
there doesn't seem to be an easy way to set that up.

Good to know.

I also didn't notice a rationale for the change. The type signatures
looked the same as for the malloc functions, so I don't know what the
difference is.

The difference is that calloc will initialize the allocated memory to zeros. Some C libraries assume this, at least freetype2 does, and since I want to use that library from Haskell I needed a binding to calloc. I assume others will run into this need eventually as well. When I send a "proposal:..." email, hopefully this weekend, I'll definitely make the rationale clear.

Re: [PATCH 1/2] add calloc and callocBytes to Foreign.Marshal.Alloc

NB: libc can usually lean on the knowledge that virtual memory retrieved from sbrk is already initialized to 0 when you calloc for memory initialized to 0, so it can be vastly more efficient than malloc + memset and it can decrease the number of useless 0 pages floating around.

On Fri, Apr 22, 2011 at 07:20:55AM -0700, Jason Dagit wrote:
>
> That wiki page doesn't say how long the discussion period should be or give
> advice on how to determine the size. What would you recommend for this
> patch?

I just updated the page to recommend 2 weeks, which I think is the
status quo.

> So it sounds like the things I missed were:
> * putting "Proposal:" in the subject line
> * setting a discussion period
> * attaching my patch to the trac instance (can I send a pull request on
> github instead?)

You don't need to make a trac ticket for it until the proposal has been
accepted.

Right, I did understand that.

Anything that means we see that the patch needs to be applied is OK.
Currently pull requests don't get sent to the mailing list though, and
there doesn't seem to be an easy way to set that up.

Good to know.

I also didn't notice a rationale for the change. The type signatures
looked the same as for the malloc functions, so I don't know what the
difference is.

The difference is that calloc will initialize the allocated memory to zeros. Some C libraries assume this, at least freetype2 does, and since I want to use that library from Haskell I needed a binding to calloc. I assume others will run into this need eventually as well. When I send a "proposal:..." email, hopefully this weekend, I'll definitely make the rationale clear.

Re: [PATCH 1/2] add calloc and callocBytes to Foreign.Marshal.Alloc

It doesn't have to be a binding. If you'd prefer to provide a Haskell implementation in terms of malloc/copyBytes that sounds fine too. I wasn't sure how to best do that so a binding seemed the most straightforward.

I was looking at this after I sent the email. copyBytes and moveBytes are bindings to memcpy and memmove, not memset.

I don't see a binding to memset in Foreign. Perhaps that should be added as well.