Skip to content

Commit

Permalink
generic_unix: simplify platform_defaultatoms handling
Browse files Browse the repository at this point in the history
Using same approach used in defaultatoms also for generic_unix platform ones.

Signed-off-by: Davide Bettio <davide@uninstall.it>
  • Loading branch information
bettio committed Dec 26, 2024
1 parent 9bbcb31 commit 5363c44
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 118 deletions.
1 change: 1 addition & 0 deletions src/platforms/generic_unix/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set(HEADER_FILES
otp_socket_platform.h
generic_unix_sys.h
mapped_file.h
platform_defaultatoms.def
platform_defaultatoms.h
../../../libAtomVM/otp_net.h
../../../libAtomVM/otp_socket.h
Expand Down
87 changes: 22 additions & 65 deletions src/platforms/generic_unix/lib/platform_defaultatoms.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,73 +20,30 @@

#include "platform_defaultatoms.h"

static const char *const proto_atom = "\x5" "proto";
static const char *const udp_atom = "\x3" "udp";
static const char *const tcp_atom = "\x3" "tcp";
static const char *const socket_atom = "\x6" "socket";
static const char *const fcntl_atom = "\x5" "fcntl";
static const char *const bind_atom = "\x4" "bind";
static const char *const getsockname_atom = "\xB" "getsockname";
static const char *const recvfrom_atom = "\x8" "recvfrom";
static const char *const recv_atom = "\x4" "recv";
static const char *const sendto_atom = "\x6" "sendto";
static const char *const send_atom = "\x4" "send";

static const char *const sta_got_ip_atom = "\xA" "sta_got_ip";
static const char *const sta_connected_atom = "\xD" "sta_connected";

static const char *const address_atom = "\x7" "address";
static const char *const controlling_process_atom = "\x13" "controlling_process";
static const char *const active_atom = "\x6" "active";
static const char *const buffer_atom = "\x6" "buffer";

static const char *const getaddrinfo_atom = "\xB" "getaddrinfo";
static const char *const no_such_host_atom = "\xC" "no_such_host";
static const char *const connect_atom = "\x7" "connect";
static const char *const tcp_closed_atom = "\xA" "tcp_closed";

static const char *const listen_atom = "\x6" "listen";
static const char *const backlog_atom = "\x7" "backlog";
static const char *const accept_atom = "\x6" "accept";
static const char *const fd_atom = "\x2" "fd";
static const char *const generic_unix_atom = "\xC" "generic_unix";
#include <stdlib.h>
#include <string.h>

void platform_defaultatoms_init(GlobalContext *glb)
{
int ok = 1;

ok &= globalcontext_insert_atom(glb, proto_atom) == PROTO_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, udp_atom) == UDP_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, tcp_atom) == TCP_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, socket_atom) == SOCKET_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, fcntl_atom) == FCNTL_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, bind_atom) == BIND_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, getsockname_atom) == GETSOCKNAME_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, recvfrom_atom) == RECVFROM_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, recv_atom) == RECV_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, sendto_atom) == SENDTO_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, send_atom) == SEND_ATOM_INDEX;

ok &= globalcontext_insert_atom(glb, sta_got_ip_atom) == STA_GOT_IP_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, sta_connected_atom) == STA_CONNECTED_ATOM_INDEX;

ok &= globalcontext_insert_atom(glb, address_atom) == ADDRESS_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, controlling_process_atom) == CONTROLLING_PROCESS_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, active_atom) == ACTIVE_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, buffer_atom) == BUFFER_ATOM_INDEX;

ok &= globalcontext_insert_atom(glb, getaddrinfo_atom) == GETADDRINFO_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, no_such_host_atom) == NO_SUCH_HOST_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, connect_atom) == CONNECT_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, tcp_closed_atom) == TCP_CLOSED_ATOM_INDEX;

ok &= globalcontext_insert_atom(glb, listen_atom) == LISTEN_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, backlog_atom) == BACKLOG_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, accept_atom) == ACCEPT_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, fd_atom) == FD_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, generic_unix_atom) == GENERIC_UNIX_ATOM_INDEX;

if (!ok) {
AVM_ABORT();
// About X macro: https://en.wikipedia.org/wiki/X_macro
#define X(name, lenstr, str) \
lenstr str,

static const char *const atoms[] = {
#include "platform_defaultatoms.def"

// dummy value
NULL
};
#undef X

for (int i = 0; i < ATOM_FIRST_AVAIL_INDEX - PLATFORM_ATOMS_BASE_INDEX; i++) {
if (UNLIKELY((size_t) atoms[i][0] != strlen(atoms[i] + 1))) {
AVM_ABORT();
}

if (UNLIKELY(globalcontext_insert_atom(glb, atoms[i]) != i + PLATFORM_ATOMS_BASE_INDEX)) {
AVM_ABORT();
}
}
}
50 changes: 50 additions & 0 deletions src/platforms/generic_unix/lib/platform_defaultatoms.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This file is part of AtomVM.
*
* Copyright 2019-2024 Davide Bettio <davide@uninstall.it>
*
* Licensed under the Apache License, Version 2.0 (the "License"))
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/

X(PROTO_ATOM, "\x5", "proto")
X(UDP_ATOM, "\x3", "udp")
X(TCP_ATOM, "\x3", "tcp")
X(SOCKET_ATOM, "\x6", "socket")
X(FCNTL_ATOM, "\x5", "fcntl")
X(BIND_ATOM, "\x4", "bind")
X(GETSOCKNAME_ATOM, "\xB", "getsockname")
X(RECVFROM_ATOM, "\x8", "recvfrom")
X(RECV_ATOM, "\x4", "recv")
X(SENDTO_ATOM, "\x6", "sendto")
X(SEND_ATOM, "\x4", "send")

X(STA_GOT_IP_ATOM, "\xA", "sta_got_ip")
X(STA_CONNECTED_ATOM, "\xD", "sta_connected")

X(ADDRESS_ATOM, "\x7", "address")
X(CONTROLLING_PROCESS_ATOM, "\x13", "controlling_process")
X(ACTIVE_ATOM, "\x6", "active")
X(BUFFER_ATOM, "\x6", "buffer")

X(GETADDRINFO_ATOM, "\xB", "getaddrinfo")
X(NO_SUCH_HOST_ATOM, "\xC", "no_such_host")
X(CONNECT_ATOM, "\x7", "connect")
X(TCP_CLOSED_ATOM, "\xA", "tcp_closed")

X(LISTEN_ATOM, "\x6", "listen")
X(BACKLOG_ATOM, "\x7", "backlog")
X(ACCEPT_ATOM, "\x6", "accept")
X(FD_ATOM, "\x2", "fd")
X(GENERIC_UNIX_ATOM, "\xC", "generic_unix")
76 changes: 23 additions & 53 deletions src/platforms/generic_unix/lib/platform_defaultatoms.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of AtomVM.
*
* Copyright 2019 Davide Bettio <davide@uninstall.it>
* Copyright 2019-2024 Davide Bettio <davide@uninstall.it>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,66 +23,36 @@

#include "defaultatoms.h"

#define PROTO_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 0)
#define UDP_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 1)
#define TCP_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 2)
#define SOCKET_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 3)
#define FCNTL_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 4)
#define BIND_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 5)
#define GETSOCKNAME_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 6)
#define RECVFROM_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 7)
#define RECV_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 8)
#define SENDTO_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 9)
#define SEND_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 10)
// About X macro: https://en.wikipedia.org/wiki/X_macro

#define STA_GOT_IP_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 11)
#define STA_CONNECTED_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 12)
#define X(name, lenstr, str) \
name##_INDEX,

#define ADDRESS_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 13)
#define CONTROLLING_PROCESS_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 14)
#define ACTIVE_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 15)
#define BUFFER_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 16)
enum
{
PLATFORM_ATOMS_BASE_INDEX_MINUS_ONE = PLATFORM_ATOMS_BASE_INDEX - 1,

#define GETADDRINFO_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 17)
#define NO_SUCH_HOST_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 18)
#define CONNECT_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 19)
#define TCP_CLOSED_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 20)
#include "platform_defaultatoms.def"

#define LISTEN_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 21)
#define BACKLOG_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 22)
#define ACCEPT_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 23)
#define FD_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 24)
#define GENERIC_UNIX_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 25)
ATOM_FIRST_AVAIL_INDEX
};

#define PROTO_ATOM term_from_atom_index(PROTO_ATOM_INDEX)
#define UDP_ATOM term_from_atom_index(UDP_ATOM_INDEX)
#define TCP_ATOM term_from_atom_index(TCP_ATOM_INDEX)
#define SOCKET_ATOM term_from_atom_index(SOCKET_ATOM_INDEX)
#define FCNTL_ATOM term_from_atom_index(FCNTL_ATOM_INDEX)
#define BIND_ATOM term_from_atom_index(BIND_ATOM_INDEX)
#define GETSOCKNAME_ATOM term_from_atom_index(GETSOCKNAME_ATOM_INDEX)
#define RECVFROM_ATOM term_from_atom_index(RECVFROM_ATOM_INDEX)
#define RECV_ATOM term_from_atom_index(RECV_ATOM_INDEX)
#define SENDTO_ATOM term_from_atom_index(SENDTO_ATOM_INDEX)
#define SEND_ATOM term_from_atom_index(SEND_ATOM_INDEX)
#undef X

#define STA_GOT_IP_ATOM term_from_atom_index(STA_GOT_IP_ATOM_INDEX)
#define STA_CONNECTED_ATOM term_from_atom_index(STA_CONNECTED_ATOM_INDEX)
_Static_assert((int) ATOM_FIRST_AVAIL_INDEX > (int) PLATFORM_ATOMS_BASE_INDEX,
"default atoms and platform ones are overlapping");

#define ADDRESS_ATOM term_from_atom_index(ADDRESS_ATOM_INDEX)
#define CONTROLLING_PROCESS_ATOM term_from_atom_index(CONTROLLING_PROCESS_ATOM_INDEX)
#define ACTIVE_ATOM term_from_atom_index(ACTIVE_ATOM_INDEX)
#define BUFFER_ATOM term_from_atom_index(BUFFER_ATOM_INDEX)
#define X(name, lenstr, str) \
name = TERM_FROM_ATOM_INDEX(name##_INDEX),

#define GETADDRINFO_ATOM term_from_atom_index(GETADDRINFO_ATOM_INDEX)
#define NO_SUCH_HOST_ATOM term_from_atom_index(NO_SUCH_HOST_ATOM_INDEX)
#define CONNECT_ATOM term_from_atom_index(CONNECT_ATOM_INDEX)
#define TCP_CLOSED_ATOM term_from_atom_index(TCP_CLOSED_ATOM_INDEX)
enum
{
#include "platform_defaultatoms.def"

#define LISTEN_ATOM term_from_atom_index(LISTEN_ATOM_INDEX)
#define BACKLOG_ATOM term_from_atom_index(BACKLOG_ATOM_INDEX)
#define ACCEPT_ATOM term_from_atom_index(ACCEPT_ATOM_INDEX)
#define FD_ATOM term_from_atom_index(FD_ATOM_INDEX)
#define GENERIC_UNIX_ATOM TERM_FROM_ATOM_INDEX(GENERIC_UNIX_ATOM_INDEX)
// dummy last item
ATOM_FIRST_AVAIL_DUMMY = TERM_FROM_ATOM_INDEX(ATOM_FIRST_AVAIL_INDEX)
};

#undef X

#endif

0 comments on commit 5363c44

Please sign in to comment.