nuttx-apps/examples/fdpicxip/modules/libcounter.c
Marco Casaroli 6e61ce7cb7 examples/fdpicxip, testing/fs/xipfs: A DT_NEEDED library is one instance.
Both the demo and the test asserted that each running instance of a module
gets its own copy of a library named in DT_NEEDED: two instances adding
their own seed each saw a total of seed*3.

That was true of the loader that walked DT_NEEDED itself.  The loader now
hands the work to dlopen(), which returns the object already in the module
registry rather than loading a second copy of it, so there is one library
and one set of its globals, shared by every module that names it.  The
module's own data stays private per instance, because exec() loads the
module afresh each time.

What an instance can still assert on its own is that every add it made
landed in the library, so that is what it checks; the totals interleave and
the final one counts both.  The test additionally checks the consequences:
the library is pinned once rather than once per instance, and its
destructor runs once, at the last close, holding what both instances built
up.

USER_FAIL_PRIVATE becomes USER_FAIL_SHARED rather than gaining a
companion.  The bit is a private protocol between cxxuser.cpp, which sets
it, and testing/fs/xipfs, which reads it; nothing else names it, and the
property it used to report no longer exists.

Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-07 11:24:32 +08:00

61 lines
2.2 KiB
C

/****************************************************************************
* apps/examples/fdpicxip/modules/libcounter.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/* A shared library with its own state.
*
* g_calls is the interesting part: it lives in the library's writable
* segment, and there is one of it. A library named in DT_NEEDED is opened
* with dlopen(), which hands back the instance already in the module
* registry, so every task that names this library counts into the same
* g_calls. The library's code, meanwhile, is mapped once and executed in
* place.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <syslog.h>
/****************************************************************************
* Private Data
****************************************************************************/
static int g_calls;
/****************************************************************************
* Public Functions
****************************************************************************/
int counter_bump(int by);
int counter_total(void);
int counter_bump(int by)
{
g_calls += by;
return g_calls;
}
int counter_total(void)
{
return g_calls;
}