2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Implementation of the user-space ashmem API for devices, which have our
19 * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version,
20 * used by the simulator.
25 #include <sys/types.h>
27 #include <sys/ioctl.h>
30 #include <linux/ashmem.h>
33 #define ASHMEM_DEVICE "/dev/ashmem"
36 * ashmem_create_region - creates a new ashmem region and returns the file
37 * descriptor, or <0 on error
39 * `name' is an optional label to give the region (visible in /proc/pid/maps)
40 * `size' is the size of the region, in page-aligned bytes
42 int ashmem_create_region(const char *name
, size_t size
)
46 fd
= open(ASHMEM_DEVICE
, O_RDWR
);
51 char buf
[ASHMEM_NAME_LEN
];
53 strlcpy(buf
, name
, sizeof(buf
));
54 ret
= ioctl(fd
, ASHMEM_SET_NAME
, buf
);
59 ret
= ioctl(fd
, ASHMEM_SET_SIZE
, size
);
70 int ashmem_set_prot_region(int fd
, int prot
)
72 return ioctl(fd
, ASHMEM_SET_PROT_MASK
, prot
);
75 int ashmem_pin_region(int fd
, size_t offset
, size_t len
)
77 struct ashmem_pin pin
= { offset
, len
};
78 return ioctl(fd
, ASHMEM_PIN
, &pin
);
81 int ashmem_unpin_region(int fd
, size_t offset
, size_t len
)
83 struct ashmem_pin pin
= { offset
, len
};
84 return ioctl(fd
, ASHMEM_UNPIN
, &pin
);
87 int ashmem_get_size_region(int fd
)
89 return ioctl(fd
, ASHMEM_GET_SIZE
, NULL
);
92 int ashmem_purge_all(void)
94 const int fd
= open(ASHMEM_DEVICE
, O_RDWR
);
97 const int ret
= ioctl(fd
, ASHMEM_PURGE_ALL_CACHES
, 0);