I certainly wouldn't encourage anybody to use negative numbers for bit mask values!

Why not just write an aligned memory allocator which stashes an extra pointer in a known location for easy freeing?

void * aligned_malloc(size_t size, size_t align)
{
    // allocate storage area with space for an extra pointer and alignment
    void * mem = malloc(size + sizeof(void*) + (align - 1));
    // point to the start of the aligned storage
    void * ptr = (void **)((uintptr_t)(mem + (align - 1) + sizeof(void *)) & ~(align - 1));     // store the address of the allocated memory prior to the aligned storage
    ((void **) ptr)[-1] = mem;
    return ptr;
}

void aligned_free(void *ptr)
{
    free(((void**) ptr)[-1]);
}


On 18/07/2018 2:59 AM, Charles Mills wrote:
The lines beginning with * aren't C either <g>. You want void *realp ...

Also, your second and third lines combine pretty well into one.

Cpool *p = (Cpool *)((long)realp & -16));

No one ever said system programming in C was pretty.

Charles


-----Original Message-----
From: IBM Mainframe Discussion List [mailto:[email protected]] On Behalf 
Of Steve Smith
Sent: Tuesday, July 17, 2018 11:44 AM
To: [email protected]
Subject: Re: S0C6 in CSRC4RG1

In assembler, STORAGE provides a BNDRY= keyword, which would be fine, but
this is a Metal C program.  malloc provides no such thing.  Don's
suggestion is doable, but it's fugly C code, and amounts to doing vascular
surgery wearing oven mitts:

*void realp = malloc(1408 + 15);
long ip = (long)realp & -16;
*Cpool p = (*CPool)ip;

Yeah, that's clear to anyone.  Although I'm sure it could be obfuscated.
Note: If you think the 1st & 3rd lines are commented out, this thread is
not for you :-)

Problems:
1. Undocumented requirement to quad-word align CPOOL anchor and/or extent
in 64-bit mode (and actually undocumented alignment requirements for all).
2. Unable to guarantee quad-word alignment with malloc.

Ugh.
sas


On Tue, Jul 17, 2018 at 1:41 PM, Don Poitras <[email protected]> wrote:

Add 15 bytes to whatever length you are asking for and copy the
pointer. 'And' the last nibble to 0. Voila! quad-aligned pointer. :)
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to