On (Thu) Mar 19 2009 [10:39:05], Daniel P. Berrange wrote:
On Thu, Mar 19, 2009 at 12:45:24PM +0530, Amit Shah wrote:
> diff --git a/src/util.c b/src/util.c
> index 66ad9a4..b69d33a 100644
> --- a/src/util.c
> +++ b/src/util.c
> @@ -117,6 +117,26 @@ ssize_t safewrite(int fd, const void *buf, size_t count)
> return nwritten;
> }
>
> +#if 1
> +int safezero(int fd, int flags, off_t offset, off_t len)
> +{
> + return posix_fallocate(fd, offset, len);
> +}
> +#else
> +int safezero(int fd, int flags, off_t offset, off_t len)
> +{
> + char *buf;
> + int r;
> +
> + buf = calloc(len, sizeof(char));
> + if (buf == NULL)
> + return -ENOMEM;
> +
> + r = safewrite(fd, buf, len);
> + return r;
> +}
> +#endif
For memory allocation you can use
if (VIR_ALLOC_N(buf, len) < 0)
return -1;
Also, you'll need a VIR_FREE(buf) call in there after the safewrite().
I'm a little worried about scalability of this impl though. The later
patch will call this with 500 MB chunks if progress is turned on, or
even allocate the whole file in one chunk - we don't want to be
allocating 20 GB of memory jus to write zero's to a file ! Is it
perhaps easier just to do something like
char * buf = mmap(NULL, len, MAP_SHARED, MAP_ANONYMOUS, fd, offset)
memset(buf, 0, len);
munmap(buf, len);
Or, do your calloc() of a 1 MB chunk, and then call safewrite in a
loop, just to avoid too large a memory allocation.
Yeah; I forgot the free().
My approach was to never allocate more than 500MiB. However we can call
safezero() itself with less than 500M; is 200M OK?
Amit