//
//  OAWrappedMemory.m
//  papers-acm
//
//  Created by Niko Matsakis on 03.07.07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "OAAutoRelAlloc.h"


@interface OAWrappedMemory : NSObject {
    void *ptr;
}
+ (void)rememberPtr:(void*)ptr;
- initWithPtr:(void*)ptr;
- (void) dealloc;
@end


@implementation OAWrappedMemory
+ (void)rememberPtr:(void*)ptr {
    [[[OAWrappedMemory alloc] initWithPtr:ptr] autorelease];
}
- initWithPtr:(void*)p {
    if ((self = [super init])) {
        ptr = p;
    }
    return self;
}
- (void) dealloc {
    free(ptr);
    [super dealloc];
}
@end

void *OAAutoRelAlloc(size_t bytes) {
    void *res = malloc(bytes);
    if (res)
        [OAWrappedMemory rememberPtr:res];
    return res;
}