Skip to content
Kazuhiro Yamato edited this page Jul 3, 2014 · 8 revisions

A library of commonly used functions.

One of the reasons why we created this library is to make code simple by wrapping C-style APIs such as sem_wait(), sprintf() etc. For example, explicit initialization or free can be automated by a constructor and a destructor. Or proper operator overload improves readability.

Classes

  • AtomicValue: Atomic operation

    • get(), set(), add(), sub(), etc.

    • internally uses gcc's builtin functions such as __sync_add_and_fetch().

    • We can use instances as if they are native type by operator's overload.

      AtomicValue<int> a, b;
      a = 1;
      b = a;

    • GCC generates the following machine code (gcc 4.8.2 x86_64 w/o optimization). The difference is little. I.e. the implementation is efficient.

      • with __sync_add_and_fetch():
        lock xadd %eax,-0x4(%rbp)

      • normal add operation like a + b;
        add %edx,%eax

  • EventSemaphore

    • A semaphore using EventFd, which is one the features of Linux.
    • We can wait for a semaphore with GLibEvent Loop based on poll().
  • Logger

    • printf like API: MLPL_X() (X: DBG, INFO, WARN, ERR, CRIT, BUG) that writes a message both to stderr and syslog.

      MLPL_INFO ("My name: %s, Age: %d\n", name, age);

  • MutexLock

    • lock(), unlock(), and timedlock().
  • ParsableString

    • We can parse a string from the top with specifying a separator.
  • ReadWriteLock

    • readLock(), writeLock(), and unlock()
  • Reaper

    • An auto pointer helper.

    • deletes an object or a buffer at the end of the block.

    • works even when an exception is thrown.

      MutexLock lock;
      Reaper unlocker(&lock, MutexLock::unlock);

      char *buf = malloc(100);
      Reaper<char *> reaper(buf, free);

  • SimpleSemaphore

    • A wrapper class of POSIX semaphore.
  • SmartBuffer

    • Utility to handle a buffer.
    • extends the size automatically.
    • provide stream-like APIs such as add8(), add16, etc.
    • helper functions to cast a pointer.
  • SmartQueue

    • MT-safe Queue: push() and pop()
    • pop() is blocked when there's no elements.
  • SmartTime: holds time (sec and nanosec)

    • Constructor(INIT_CURR_TIME)
    • Constructor(const timespec &ts)
    • Comparators: <, >, <=, >=, ==
    • Add/Sub: +=, -=
  • StringUtils

    • Helper functions related to string.
    • sprintf()
    • split()
    • casecmp()
    • isNumber()
    • eraseChars()