ObjFW  Artifact [9fc0378e5d]

Artifact 9fc0378e5da40f7bf72764490510afc1b898f9d8f7d13ba49fdf47e921085591:

  • File src/OFObject.h — part of check-in [57fb5578cc] at 2009-04-19 17:37:52 on branch trunk — Remove forwarding methods. See long commit message for details.

    There are two resons for removing it:

    First, OFPlugin does not need forwarding anymore. Second is that
    forwarding is broken in both, the GNU and the Apple runtime.

    In GNU libobjc, objc_msg_sendv is implemented using __builtin_apply,
    which is broken on many platforms, including x86_64. If forwarding is
    used, the application will just crash. To work around that, I'd need to
    parse the type encoding and use libffi to call the method instead of
    using objc_msg_sendv.

    Now the Apple runtime has a similar problem: There is no objc_msgSendv
    for PPC64 and x86_64 as it's only in ObjC1 and on ARM (iPhone), it's
    broken (most likely because the iPhone uses only ObjC2 - I was confused
    that objc_msgSendv was even in the libobjc there). So I'd need to write
    an ASM implementation for these 3.

    Writing those 3 ASM implementations (or 5, so we don't depend on ObjC1
    stuff on PPC32 and x86 as well) wouldn't be a problem, but there is a
    problem the GNU libobjc and the Apple runtime got in common, which
    originates from the early ObjC implementations:

    forward:: and performv:: were only designed to return scalar types. But
    today, it's possible to return floats, structs and unions as well. What
    Apple and GNU use here is a very hacky workaround and it's just luck
    that it works. forward:: and performv:: both return an id (Apple) or
    void* (GNU). forward:: is called by the runtime if you called a method
    that is not implemented. The compiler does not know at compile time
    that it is not implemented, therefore expects a float as a return. On
    x86, floats are returned in sp0. The runtime now notices that the
    called method is not implemented and calls forward::. Forward then
    calls performv:: to call the right method. The method returns a float
    and stores it in sp0. Remember that both, forward:: and performv::
    return an id / void*. performv:: returns now and after that, forward::
    returns. The return of those was always put into eax, as that's how
    scalar values are returned on x86. The original caller of the method
    does not expect any return value in eax, but in sp0. This works, as
    no code touched sp0. However, you can not rely on sp0 not being
    touched. It's just luck that the compiler generates code that does not
    touch sp0.

    While this works for forwarding due to the ABI on x86 (and the ABIs on
    many other platforms allow this hack as well), this fails if you call
    performv:: directly on a method returning a float. In this case, the
    compiler does not expect a return value in sp0, but in eax, as
    performv:: is expected to return id / void*. Therefore the bogus value
    in eax will be casted to float and the result will be useless.

    This is why I decided to remove forwarding and performv:: from libobjfw
    for now. If I encounter a situation where I need forwarding, I'm going
    to implement it in a sane way and NOT the objc way. The forwarding
    methods this commit removes did it the objc way, which is IMO just
    wrong. (That way was ok back then when you only had scalar return
    types, but today you're not limited to scalar return types anymore.) (user: js, size: 4765) [annotate] [blame] [check-ins using]


/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include <stddef.h>
#include <stdint.h>

#import <objc/objc.h>
#ifndef __objc_INCLUDE_GNU
#import <objc/message.h>
#endif

/**
 * The OFObject class is the base class for all other classes inside ObjFW.
 */
@interface OFObject
{
	Class  isa;
}

/**
 * This code is executed once when a method of the class is called for the first
 * time.
 * Derived classes can override this to execute their own code on
 * initialization.
 */
+ initialize;

/**
 * Allocates memory for an instance of the class.
 */
+ alloc;

/**
 * Allocated memory for an instance of the class and initializes the instance.
 */
+ new;

/**
 * \return The class pointer
 */
+ (Class)class;

/**
 * \return The name of the class as a C string
 */
+ (const char*)name;

/**
 * Replace a method with a method from another class.
 *
 * \param selector The selector of the method to replace
 * \param class The class from which the new method should be taken
 * \return The old implementation
 */
+ (IMP)replaceMethod: (SEL)selector
 withMethodFromClass: (Class)class;

/**
 * Initialize the already allocated object.
 * Also sets up the memory pool for the object.
 *
 * \return An initialized object
 */
- init;

/**
 * \return A pointer to the class of the instance
 */
- (Class)class;

/**
 * \return The name of the instance's class as a C string
 */
- (const char*)name;

/**
 * \param class The class whose kind is checked
 *
 * \return A boolean whether the object is of the specified kind
 */
- (BOOL)isKindOf: (Class)class;

/**
 * \param selector The selector which should be checked
 *
 * \return A boolean whether the objects responds to the specified selector
 */
- (BOOL)respondsTo: (SEL)selector;

/**
 * \param selector The selector for which the method should be returned
 *
 * \return The implementation for the specified selector
 */
- (IMP)methodFor: (SEL)selector;

/**
 * Compare two objects.
 * Classes containing data (like strings, arrays, lists etc.) should reimplement
 * this!
 *
 * \param obj The object which is tested for equality
 * \return A boolean whether the object is equal to the other object
 */
- (BOOL)isEqual: (id)obj;

/**
 * Calculate a hash for the object.
 * Classes containing data (like strings, arrays, lists etc.) should reimplement
 * this!
 *
 * \return A 32 bit hash for the object
 */
- (uint32_t)hash;

/**
 * Adds a pointer to the memory pool.
 * This is useful to add memory allocated by functions such as asprintf to the
 * pool so it gets freed automatically when the object is freed.
 *
 * \param ptr A pointer to add to the memory pool
 */
- addToMemoryPool: (void*)ptr;

/**
 * Allocate memory and store it in the objects memory pool so it can be free'd
 * automatically when the object is free'd.
 *
 * \param size The size of the memory to allocate
 * \return A pointer to the allocated memory
 */
- (void*)getMemWithSize: (size_t)size;

/**
 * Allocate memory for a specified number of items and store it in the objects
 * memory pool so it can be free'd automatically when the object is free'd.
 *
 * \param nitems The number of items to allocate
 * \param size The size of each item to allocate
 * \return A pointer to the allocated memory
 */
- (void*)getMemForNItems: (size_t)nitems
		  ofSize: (size_t)size;

/**
 * Resize memory in the memory pool to a specified size.
 *
 * \param ptr A pointer to the already allocated memory
 * \param size The new size for the memory chunk
 * \return A pointer to the resized memory chunk
 */
- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size;

/**
 * Resize memory in the memory pool to a specific number of items of a
 * specified size.
 *
 * \param ptr A pointer to the already allocated memory
 * \param nitems The number of items to resize to
 * \param size The size of each item to resize to
 * \return A pointer to the resized memory chunk
 */
- (void*)resizeMem: (void*)ptr
	  toNItems: (size_t)nitems
	    ofSize: (size_t)size;

/**
 * Frees allocated memory and removes it from the memory pool.
 *
 * \param ptr A pointer to the allocated memory
 */
- freeMem: (void*)ptr;

/**
 * Increases the retain count.
 */
- retain;

/**
 * Adds the object to the autorelease pool that is on top of the thread's stack.
 */
- autorelease;

/**
 * \return The retain count
 */
- (size_t)retainCount;

/**
 * Decreases the retain cound and frees the object if it reaches 0.
 */
- (void)release;

/**
 * Frees the object and also frees all memory allocated via its memory pool.
 */
- free;
@end