refactor roanon_mmap() & memory alloc/free checks & logging (#77)

memory logging and fix mmap() leak plus some cleanups

- implement optional logging of memory allocs & frees
- refactor roanon_mmap() by moving its mmap() call to platform-zos.cc
  where it is now tracked for correct memory release, otherwise that
  mmap() caused a memory leak
- add more checking and reporting when allocating and freeing memory
- remove lock from ~__Cache(), since the function is called only during exit-time
- return MAP_FAILED from anon_mmap rather than nullptr,
  as that's what's expected from mmap().

Co-authored-by: Igor Todorovski <itodorov@ca.ibm.com>
2 files changed
tree: dcc149c6269e3c8c479359f083764c6ee83e550e
  1. docs/
  2. include/
  3. latex/
  4. mainpage/
  5. man/
  6. scripts/
  7. src/
  8. test/
  9. .clang-format
  10. .gitignore
  11. CMakeLists.txt
  12. LICENSE
  13. README.md
  14. zoslib.gyp
README.md

ZOSLIB - A z/OS C/C++ Library

Table of Contents

Overview

ZOSLIB is a z/OS C/C++ library. It is an extended implementation of the z/OS LE C Runtime Library.

ZOSLIB implements the following:

  • A subset of POSIX APIs that are not available in the LE C Runtime Library
  • EBCDIC <-> ASCII conversion C APIs
  • APIs for improved diagnostic reporting
  • and more!

System Requirements

ZOSLIB is supported on the following z/OS operating systems with z/OSĀ® UNIX System Services enabled:

  • z/OS V2R3 with the following PTFs installed:

  • UI61308

  • UI61375

  • UI61747

  • z/OS V2R4 with the following PTFs installed:

  • UI64830

  • UI64837

  • UI64839

  • UI64940

  • UI65567

ZOSLIB is supported on the following hardware:

  • IBM z15
  • IBM z14/z14 Model ZR1
  • IBM z13/z13s
  • IBM zEnterprise EC12/BC12

Build and Install

Build tool prerequisites

  • CMake 3.5+
  • GNU Make 4.1+
  • IBM XL C/C++ V2.3.1 for z/OS V2.3 web deliverable (xlclang/xlcang++)
  • Git
  • Ninja (optional)

Clone the ZOSLIB source code using Git into the newly created zoslib directory:

$ git clone git@github.com:ibmruntimes/zoslib.git zoslib

After obtaining the source, cd to the zoslib directory and create a build directory to hold your build files:

$ cd zoslib
$ mkdir mybuilddir && cd mybuilddir

Next, we will configure our build with CMake.

Make sure to export the CC and CXX environment variables to point to the supported C/C++ build compiler, or pass in the CMake options -DCMAKE_C_COMPILER and -DCMAKE_CXX_COMPILER.

From the directory mybuilddir, enter the following CMake command (here, .. refers to the ZOSLIB source directory)

$ cmake ..

By default CMake will configure your build as a Debug build. You can configure your build as a Release build with the -DCMAKE_BUILD_TYPE=Release option.

CMake will detect your development environment, perform a series of tests, and generate the files required for building ZOSLIB.

By default, CMake will generate Makefiles. If you prefer to use Ninja, you can specify -GNinja as an option to CMake.

After CMake has finished with the configuration, start the build from mybuilddir using CMake:

$ cmake --build .

After ZOSLIB has finished building, install it from mybuilddir:

$ cmake --build . --target install

Quick Start

Once we have ZOSLIB built and installed, let's attempt to build our first ZOSLIB C++ application. The application will generate a series of random numbers, leveraging the getentropy C API in ZOSLIB.

  1. Create a file named random.cc containing the following contents:
// random.cc

// Include zos.h ZOSLIB header
#include <zos.h>
#include <stdio.h>

// Initialize ZOSLIB class
__init_zoslib __nodezoslib;

int main(int argc,  char** argv) {
  printf("ZOSLIB version: %s\n", __zoslib_version);
  if (argc < 1) {
    printf("An argument specifying the number of random "
           " numbers is required\n");
    return 1;
  }

  int num = atoi(argv[1]);
  if (num < 0) {
    printf("The argument should be positive (>0)\n");
    return 2;
  }
  printf("Generating %d random values\n", num);

  char buffer[10];
  for (int i = 0; i < num; i++) {
    printf("Random index: %d\n", i);
    // Call ZOSLIB getentropy C API
    if (!getentropy(buffer, 10)) {
      for (int j = 0; j < 10; j++)
        printf("%2X ", buffer[j]);
      printf("\n");
    }
  }
  return 0;
}

This example will first include the main ZOSLIB header file, zos.h, which subsequently includes all of the ZOSLIB header files. Alternatively, we could have just included zos-base.h, since the prototype for getentropy is defined in zos-base.h.

  1. In order to initialize ZOSLIB, we need to create a static instance of the __init_zoslib class: __init_zoslib zoslib_init;. This initializes the Enhanced ASCII runtime environment, among other things. If your application is C only, you can make use of the init_zoslib function instead.

In the main function, we make use of two ZOSLIB definitions, __zoslib_version to obtain the ZOSLIB version, and getentropy to generate a list of random values.

  1. To compile and link the application, enter the following command:
xlclang++ -I path/to/zoslib/include -L path/to/mybuilddir/lib -lzoslib random.cc -o random
  1. To run the application, enter the following command:
./random 2

You should get an output similar to the following:

ZOSLIB version: v1.1.0
Generating 2 random values
Random index: 0
BC DE CF DE  7 E3 58 3A 4F 22
Random index: 1
5B 30 5A 9C C4 70 94 A6 B6 E5

API Documentation

The ZOSLIB API documentation is available here.

Legalities

ZOSLIB is available under the Apache 2.0 license. See the LICENSE file for details

Copyright

Licensed Materials - Property of IBM
ZOSLIB
(C) Copyright IBM Corp. 2020. All Rights Reserved.
US Government Users Restricted Rights - Use, duplication
or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.