system/corennnnn
Revision | 8f13782e7b4a705484d7f97f07513781b82be0dc (tree) |
---|---|
Zeit | 2009-05-21 06:51:48 |
Autor | Mathias Agopian <mathias@goog...> |
Commiter | Mathias Agopian |
move native_handle stuff from master_gl
@@ -17,12 +17,55 @@ | ||
17 | 17 | #ifndef NATIVE_HANDLE_H_ |
18 | 18 | #define NATIVE_HANDLE_H_ |
19 | 19 | |
20 | +#include <sys/cdefs.h> | |
21 | + | |
22 | +__BEGIN_DECLS | |
23 | + | |
20 | 24 | typedef struct |
21 | 25 | { |
22 | - int version; /* sizeof(native_handle) */ | |
26 | + int version; /* sizeof(native_handle_t) */ | |
23 | 27 | int numFds; /* number of file-descriptors at &data[0] */ |
24 | 28 | int numInts; /* number of ints at &data[numFds] */ |
25 | 29 | int data[0]; /* numFds + numInts ints */ |
26 | -} native_handle; | |
30 | +} native_handle_t; | |
31 | + | |
32 | + | |
33 | +/* keep the old definition for backward source-compatibility */ | |
34 | +typedef native_handle_t native_handle; | |
35 | + | |
36 | +/* | |
37 | + * native_handle_close | |
38 | + * | |
39 | + * closes the file descriptors contained in this native_handle_t | |
40 | + * | |
41 | + * return 0 on success, or a negative error code on failure | |
42 | + * | |
43 | + */ | |
44 | +int native_handle_close(const native_handle_t* h); | |
45 | + | |
46 | + | |
47 | +/* | |
48 | + * native_handle_create | |
49 | + * | |
50 | + * creates a native_handle_t and initializes it. must be destroyed with | |
51 | + * native_handle_delete(). | |
52 | + * | |
53 | + */ | |
54 | +native_handle_t* native_handle_create(int numFds, int numInts); | |
55 | + | |
56 | +/* | |
57 | + * native_handle_delete | |
58 | + * | |
59 | + * frees a native_handle_t allocated with native_handle_create(). | |
60 | + * This ONLY frees the memory allocated for the native_handle_t, but doesn't | |
61 | + * close the file descriptors; which can be achieved with native_handle_close(). | |
62 | + * | |
63 | + * return 0 on success, or a negative error code on failure | |
64 | + * | |
65 | + */ | |
66 | +int native_handle_delete(native_handle_t* h); | |
67 | + | |
68 | + | |
69 | +__END_DECLS | |
27 | 70 | |
28 | 71 | #endif /* NATIVE_HANDLE_H_ */ |
@@ -20,6 +20,7 @@ commonSources := \ | ||
20 | 20 | array.c \ |
21 | 21 | hashmap.c \ |
22 | 22 | atomic.c \ |
23 | + native_handle.c \ | |
23 | 24 | buffer.c \ |
24 | 25 | socket_inaddr_any_server.c \ |
25 | 26 | socket_local_client.c \ |
@@ -0,0 +1,60 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2007 The Android Open Source Project | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | + | |
17 | +#define LOG_TAG "NativeHandle" | |
18 | + | |
19 | +#include <stdint.h> | |
20 | +#include <errno.h> | |
21 | +#include <string.h> | |
22 | +#include <stdlib.h> | |
23 | +#include <unistd.h> | |
24 | + | |
25 | +#include <cutils/log.h> | |
26 | +#include <cutils/native_handle.h> | |
27 | + | |
28 | +native_handle_t* native_handle_create(int numFds, int numInts) | |
29 | +{ | |
30 | + native_handle_t* h = malloc( | |
31 | + sizeof(native_handle_t) + sizeof(int)*(numFds+numInts)); | |
32 | + | |
33 | + h->version = sizeof(native_handle_t); | |
34 | + h->numFds = numFds; | |
35 | + h->numInts = numInts; | |
36 | + return h; | |
37 | +} | |
38 | + | |
39 | +int native_handle_delete(native_handle_t* h) | |
40 | +{ | |
41 | + if (h) { | |
42 | + if (h->version != sizeof(native_handle_t)) | |
43 | + return -EINVAL; | |
44 | + free(h); | |
45 | + } | |
46 | + return 0; | |
47 | +} | |
48 | + | |
49 | +int native_handle_close(const native_handle_t* h) | |
50 | +{ | |
51 | + if (h->version != sizeof(native_handle_t)) | |
52 | + return -EINVAL; | |
53 | + | |
54 | + const int numFds = h->numFds; | |
55 | + int i; | |
56 | + for (i=0 ; i<numFds ; i++) { | |
57 | + close(h->data[i]); | |
58 | + } | |
59 | + return 0; | |
60 | +} |