Developing Applications with APR Paul Querna pquerna@apache.org - - PowerPoint PPT Presentation

developing applications with apr
SMART_READER_LITE
LIVE PREVIEW

Developing Applications with APR Paul Querna pquerna@apache.org - - PowerPoint PPT Presentation

Developing Applications with APR Paul Querna pquerna@apache.org July 21, 2005 http://www.outoforder.cc/presentations/ A.P.R. Apache Portable Runtime Origin: httpd Platforms: Unix, Netware, OS/2, Windows. Your Application APR-Util


slide-1
SLIDE 1

Developing Applications with APR

Paul Querna pquerna@apache.org July 21, 2005 http://www.outoforder.cc/presentations/

slide-2
SLIDE 2

A.P.R.

  • Apache Portable Runtime
  • Origin: httpd
  • Platforms: Unix, Netware, OS/2, Windows.
slide-3
SLIDE 3

Your Application APR-Util APR Operating System Libraries

slide-4
SLIDE 4

Integration Points

  • Modules
  • Wrap Areas that use APR/Pools
  • C++ Objects
  • Entire Application
  • Memory pools everywhere
slide-5
SLIDE 5

Layers

  • Memory Allocation
  • OS Portability Functions
  • Library/Utility Functions (APR-Util)
slide-6
SLIDE 6

Memory Allocation.

  • Memory Pools
  • No free()
  • Less overhead than Garbage Collection
  • Still requires thinking!
  • Loops
  • Long running tasks
slide-7
SLIDE 7

apr_pool_create(&pool, NULL); for (i=0; i > n; ++i) { do_something(pool, i); apr_pool_clear(pool); } apr_pool_destroy(&pool, NULL);

slide-8
SLIDE 8

3.75 7.50 11.25 15.00

App Use Pool Size

slide-9
SLIDE 9

Not Perfect.

  • Unbounded Memory Usage:
  • ‘leaks’
  • Pools will not free() to the OS.
  • Can be handled with sub-pools.
slide-10
SLIDE 10

18.75 37.50 56.25 75.00

App Use Pool Size

slide-11
SLIDE 11

void use_lots_of_ram(apr_pool_t* pool) { do { void* data = apr_palloc(pool, 500); .... } while(cond_true()); }

slide-12
SLIDE 12

void use_lots_of_ram(apr_pool_t* pool) { apr_pool_create(&subpool, pool); do { void* data = apr_palloc(subpool, 500); ... } while(cond_true()); apr_pool_destroy(subpool); }

slide-13
SLIDE 13

void use_lots_of_ram(apr_pool_t* pool) { apr_pool_create(&subpool, pool); do { void* data = apr_palloc(subpool, 500); ... apr_pool_clear(subpool); } while(cond_true()); apr_pool_destroy(subpool); }

slide-14
SLIDE 14
slide-15
SLIDE 15
slide-16
SLIDE 16

Integration Points

  • Modules
  • Wrap Areas that use APR/Pools
  • C++ Objects
  • Entire Application
  • Memory pools everywhere
slide-17
SLIDE 17

Simple Applications

  • Few Calls to initialize APR
  • Cleanup
slide-18
SLIDE 18

#include "apr.h" #include "apr_file_io.h" int main(int argc, char *argv[]) { apr_pool_t *p; apr_file_t *fp; apr_initialize(); atexit(apr_terminate); apr_pool_create(&p, NULL); apr_file_open_stdout(&fp, p); apr_file_printf(fp, "Hello World\n”); return 0; }

slide-19
SLIDE 19

Data Structures

  • apr_array_header_t
  • apr_table_t
  • apr_hash_t
  • APR Rings
slide-20
SLIDE 20

Arrays

  • apr_array_make()
  • initial size
  • apr_array_push()
  • returns pointer to new memory
  • nelts
slide-21
SLIDE 21

Tables

  • Keys &

Values

  • Used for HTTP Headers
  • apr_table_do
slide-22
SLIDE 22

Hashes

  • Keys &

Values

  • Arbitrary (void*)
slide-23
SLIDE 23

Rings

  • Double Linked List
  • Optional Debugging
slide-24
SLIDE 24

apr_status_t

  • 0 == APR_SUCCESS
  • Always use APR_STATUS_IS_* macros
slide-25
SLIDE 25

File IO

  • read, write, writev, sendfile, seek, locking
  • functions for stderr/stdout/stdin
slide-26
SLIDE 26

Network IO

  • read, write, writev, sendfile
  • sockaddr
  • pollset
  • kqueue/epoll/event ports
slide-27
SLIDE 27

Threading

  • Similar to pthreads
  • threads
  • mutex
  • thread
  • process
  • global
slide-28
SLIDE 28

APR-Util

  • Everything that doesn’t have a home?
slide-29
SLIDE 29

Bucket Brigades

  • Complicated -- entire sessions in past AC

are on them

  • Buckets contain:
  • Files
  • Pipes
  • Sockets
  • Custom
  • Uses APR_RING_*
slide-30
SLIDE 30

DBM

  • iterate, fetch, insert
  • must be explicitly closed -- no cleanup
slide-31
SLIDE 31

apr_reslist

  • Pooling of Resources made easy
  • Proxy
  • DBI
  • Timeouts
  • Creation
  • Destruction
slide-32
SLIDE 32

apr_dbd

  • One API:
  • MySQL
  • Postrgres
  • SQLite v2 & v3
  • Oracle (in progress)
slide-33
SLIDE 33

apr_dbd_driver_t* driver = NULL; apr_dbd_t* handle = NULL; apr_dbd_get_driver(pool, "sqlite3", &driver); apr_dbd_open(driver, pool, “db", &handle); apr_dbd_query(driver, handle, &nrows, sql);

slide-34
SLIDE 34

Questions?

  • http://www.outoforder.cc/presentations/