Hi,

as there is no memalign (or posix_memalign) function on the i I am trying to roll my own. Does anybody sees some obvious mistake in my posix_memalign implementation?

I used this as a base for implementation: http://linux.die.net/man/3/posix_memalign

Thanx for any advice


/**
* \brief POSIX memalign implementation
*
* \author Mihael Schmidt
* \date 10.12.2010
*/

H dftactgrp(*no) actgrp(*caller)

D main PR extpgm('MEMALIGN')
D main PI


D posix_memalign...
D PR 10I 0 extproc('posix_memalign')
D pointer *
D alignment 10I 0 value
D size 10I 0 value
*
D isPowerOf2 PR N
D number 10I 0 const
*
D isMultipleOf PR N
D number 10I 0 const
D multiple 10I 0 const


D POINTER_SIZE C 16
D EINVAL C 3021
D ENOMEM C 3460
D SUCCESS C 0

D ptr S *
D value S * based(ptr)
D rc S 10I 0
/free
ptr = %alloc(%size(ptr));
rc = posix_memalign(ptr : 16 : 64);
if (rc = SUCCESS);
dsply 'yes';
dealloc value;
else;
dsply %char(rc);
endif;

dealloc ptr;

*inlr = *on;
return;
/end-free


P posix_memalign...
P B
D PI 10I 0
D pointer *
D alignment 10I 0 value
D size 10I 0 value
*
D ptr S * based(pointer)
/free
if (isPowerOf2(size) and isMultipleOf(size : alignment) and
isMultipleOf(size : POINTER_SIZE));

monitor;
ptr = %alloc(size);
return 0;

on-error;
return ENOMEM;
endmon;

else;
return EINVAL;
endif;
/end-free
P E


/**
* \brief Check if numer is power of 2
*
* \author Mihael Schmidt
* \date 2010-12-10
*
* \param Number
*
* \return *on = number is power of 2 <br>
* *off = number is not power of 2
*/
P isPowerOf2 B
D PI N
D number 10I 0 const
*
D start S 10I 0
D result S 10I 0
/free
dow (result <= number);
start += 1;
result = 2 ** start;
if (result = number);
return *on;
endif;
enddo;

return *off;
/end-free
P E


/**
* \brief Check for multiple of
*
* Checks if the first parameter is a multiple of the second parameter.
*
* \author Mihael Schmidt
* \date 2010-12-10
*
* \param Target number
* \param number
*
* \return *on = number is multiple of second parameter <br>
* *off = number is not multiple of second parameter
*/
P isMultipleOf B
D PI N
D number 10I 0 const
D multiple 10I 0 const
*
D remainder S 10I 0
/free
remainder = %rem(number : multiple);
return (remainder = 0);
/end-free
P E





Mihael Schmidt
Anwendungsentwicklung

Dirk Rossmann GmbH
Iserhägener Str.16
30938 Burgwedel
* +49 (05139) 898 - 4353

Handelsregister-Nr. HRB 120546, Amtsgericht Hannover
Ust-Id-Nr. DE 115055186
St.-Nr. 16 / 205 / 65401
Geschäftsführer: Dirk Roßmann, Alice Schardt-Roßmann, Roland Frobel, Klaus Praus



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.