am 2708f19b: Merge "Update API level 9 sysroot to allow static C++ destructors to be called on dlclose()." into gingerbread

Merge commit '2708f19ba33163b1ac0c5ea2758d0c09d97e8cc0' into gingerbread-plus-aosp

* commit '2708f19ba33163b1ac0c5ea2758d0c09d97e8cc0':
  Update API level 9 sysroot to allow static C++ destructors to be called on dlclose().
This commit is contained in:
David 'Digit' Turner
2010-07-07 14:16:52 -07:00
committed by Android Git Automerger
13 changed files with 117 additions and 0 deletions

4
ndk/.gitignore vendored
View File

@@ -2,5 +2,9 @@ samples/*/libs/
samples/*/obj/
samples/*/bin/
samples/*/gen/
tests/*/libs
tests/*/obj/
tests/*/bin/
tests/*/gen/
local.properties
build.xml

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,12 @@
# A small sample used to demonstrate static C++ destructors
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libtest1
LOCAL_SRC_FILES := libtest1.cpp
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := test_dlclose_destruction
LOCAL_SRC_FILES := main.c
include $(BUILD_EXECUTABLE)

View File

@@ -0,0 +1 @@
APP_PLATFORM := android-9

View File

@@ -0,0 +1,31 @@
#include <stddef.h>
#include "libtest1.h"
class Foo
{
public:
Foo() { mAddress = NULL; }
void setAddress(int *px);
~Foo();
private:
int *mAddress;
};
void Foo::setAddress(int *px)
{
mAddress = px;
*mAddress = 1;
}
Foo::~Foo()
{
if (mAddress)
*mAddress = 2;
}
static Foo foo;
extern "C" void test1_set(int *px)
{
foo.setAddress(px);
}

View File

@@ -0,0 +1,21 @@
#ifndef LIBTEST1_H
#define LIBTEST1_H
#ifdef __cplusplus
extern "C" {
#endif
/* define in libtest1, will be called dynamically through dlsym()
* by main.c. This function receives the address of an integer
* and sets its value to 1.
*
* when the library is unloaded, the value is set to 2 automatically
* by the destructor there.
*/
extern void test1_set(int *px);
#ifdef __cplusplus
}
#endif
#endif /* LIBTEST1_H */

View File

@@ -0,0 +1,48 @@
#include <stdio.h>
#include <dlfcn.h>
typedef void (*test_func_t)(int *px);
int x;
int main(void)
{
void* lib = dlopen("libtest1.so", RTLD_NOW);
test_func_t test_func;
if (lib == NULL) {
fprintf(stderr, "Can't load library: %s\n", dlerror());
return 1;
}
printf("Loaded !\n");
test_func = dlsym(lib, "test1_set");
if (test_func == NULL) {
fprintf(stderr, "Can't find test function\n");
return 2;
}
x = 0;
test_func(&x);
if (x == 1) {
printf("Test function called !\n");
} else {
fprintf(stderr, "Test function failed to set variable !\n");
return 3;
}
dlclose(lib);
printf("Unloaded !\n");
if (x == 2) {
printf("Test destructor called !\n");
} else if (x == 1) {
fprintf(stderr, "Test destructor was *not* called !\n");
return 4;
} else {
fprintf(stderr, "Test destructor called but returned invalid value (%d)\n", x);
return 5;
}
return 0;
}