Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 08511d6e authored by Mathieu Chartier's avatar Mathieu Chartier
Browse files

Make host ashmem_create_region seed only once.

Not seeding this each call should help reduce collisions when multiple
threads are calling ashmem_create_region. Also cleaned up code by
deleting gotos, and making formatting consistent.

Bug: 15394258

(cherry picked from commit 1104ae8c)

Change-Id: Iafdaea57b2317e0eb7c40d7b5595c523814fd88c
parent b311ca9e
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -40,9 +40,6 @@ commonSources := \
	iosched_policy.c \
	str_parms.c \

commonHostSources := \
        ashmem-host.c

# some files must not be compiled when building against Mingw
# they correspond to features not used by our host development tools
# which are also hard or even impossible to port to native Win32
@@ -69,6 +66,9 @@ ifneq ($(WINDOWS_HOST_ONLY),1)
        socket_network_client.c \
        sockets.c \

    commonHostSources += \
        ashmem-host.c

endif


+63 −61
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -36,6 +38,11 @@
#define __unused __attribute__((__unused__))
#endif

static pthread_once_t seed_initialized = PTHREAD_ONCE_INIT;
static void initialize_random() {
    srand(time(NULL) + getpid());
}

int ashmem_create_region(const char *ignored __unused, size_t size)
{
    static const char txt[] = "abcdefghijklmnopqrstuvwxyz"
@@ -44,10 +51,10 @@ int ashmem_create_region(const char *ignored __unused, size_t size)
    unsigned int retries = 0;
    pid_t pid = getpid();
    int fd;

	srand(time(NULL) + pid);

retry:
    if (pthread_once(&seed_initialized, &initialize_random) != 0) {
        return -1;
    }
    do {
        /* not beautiful, its just wolf-like loop unrolling */
        snprintf(name, sizeof(name), "/tmp/android-ashmem-%d-%c%c%c%c%c%c%c%c",
        pid,
@@ -64,20 +71,15 @@ retry:
        fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
        if (fd == -1) {
            /* unlikely, but if we failed because `name' exists, retry */
		if (errno == EEXIST && ++retries < 6)
			goto retry;
            if (errno != EEXIST || ++retries >= 6) {
                return -1;
            }

        }
    } while (fd == -1);
    /* truncate the file to `len' bytes */
	if (ftruncate(fd, size) == -1)
		goto error;

	if (unlink(name) == -1)
		goto error;

    if (ftruncate(fd, size) != -1 && unlink(name) != -1) {
        return fd;
error:
    }
    close(fd);
    return -1;
}