06
Sun, Oct
2 New Articles

Put It in Storage

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Time is a tough beast to corral. You need time to assign new projects, and then, you need to find the time to actually develop those projects. Of course, you need to make time to complete those projects, too. The use of prewritten modules and procedures that can be easily accessed from the developer’s program can help reduce the amount of time needed to develop and maintain projects and can make putting a bit in the beast’s mouth much easier. In this article, I tell you about a set of procedures I wrote to handle one common function: memory management. Your staff will be able to use this toolkit to help themselves quickly write reusable code without having to understand the complexities of memory management.

The ILE makes it easier for AS/400 programmers to create reusable procedures to be used as building blocks of programs. Toolkits provide the building blocks that support commonly used application tasks. The Storage Toolkit provides tools that make it easier to work with dynamically allocated storage. What is dynamically allocated storage? Dynamically allocated storage allows your applications to be more flexible by making it less likely that you will need to change your applications to accommodate business growth. Storage is allocated as it is needed.

Another advantage of using the Storage Toolkit is that storage can be named. Named storage allows programs to share information without passing parameters. For example, you can use the Storage Toolkit to store a named value, such as default output queue, using the Allocate Named Storage procedure. Programs that need to use this common value simply request it by name using the Retrieve Named Storage procedure. If the named storage is allocated from the local data area (LDA) or a user space, programs running in different activation groups can even share it.

You may have heard that using dynamically allocated storage is dangerous because memory leaks can reduce available system resources. A memory leak is caused when storage is allocated but never deallocated. You can use the logging facility in the memory toolkit to reduce this possibility. When logging is in effect, a message is sent when storage is allocated and deallocated. That way, you can use these messages to identify and correct leaks when you are developing a program.

Types of Storage

The Storage Toolkit supports three types of storage: heap storage, user space storage, and the LDA. Each type of storage has its own advantages and disadvantages. The one you choose depends on whether applications will share the storage, the amount of storage required, and whether submitted jobs will use the value being stored. The type and attributes of allocated storage are set when the storage is allocated and can be changed by deallocating and reallocating the storage.

Heap storage (heaps are associated with jobs and activation groups) is storage allocated from a heap. For this reason, when a job or activation group ends, heap storage is discarded. The AS/400 has two types of heaps: the default heap and user-created heaps. The Storage Toolkit uses the default heap, which RPG IV programs automatically create and maintain when you use the ALLOC, DEALLOC, and REALLOC operation codes. Heap storage can be quite large; it can expand in size to just a little less than 4 GB.

The next type of storage the Storage Toolkit supports is user space storage. User spaces are system objects that can contain up to 16 MB of data. The Storage Toolkit creates user spaces in QTEMP.

The final type of storage the Storage Toolkit supports is the LDA. The LDA is a special data area the system creates when a job starts. The biggest limitation of the LDA is its size, which is only 1024 bytes. For interactive jobs, this area is initially set to blanks. For submitted jobs, the LDA’s value is initialized to the value of the submitting job’s LDA. This fact makes the LDA a good choice when information needs to be made available to a submitted job. To display the LDA or use the LDA in a program, specify the special value *LDA for the data area name. When deciding which type of storage to use, you need to determine how much storage is necessary (a little or a lot) and whether the storage will be shared.

Allocating Storage

Before using storage with the Storage Toolkit, you need to allocate the storage. There are several ways to allocate storage using the Storage Toolkit. You can allocate storage by name or by simply retrieving a pointer to the allocated storage. You can also allocate an array of storage, including an optional header area. Three procedures are provided to allocate storage, which are listed in Figure 1. You can download the source for the listed procedures along with a demonstration program at www.midrangecomputing.com/mc/. The demonstration program, TstStgTkt, shows how to use the Storage Toolkit to allocate different types of storage.

When the Storage Toolkit allocates storage, information describing the storage and a pointer are saved to the allocated storage. The storage allocation procedures also support a logging flag. If this flag is not passed, the default storage logging value is used. When logging is requested, a message is sent when storage is allocated and deallocated. The default storage logging value is stored in data area DftStgLog. A logging value of 1 indicates that allocations and deallocations of storage should be logged. If storage is allocated to a user space, the Storage Toolkit creates the user space if the user space does not exist. The Storage Toolkit also increases the size of the user space as needed when storage is allocated. To allocate storage to a field, use either the Allocate Storage or Allocate Named Storage procedure. The amount of storage to allocate should correspond to the size of the variable that you will use to access the dynamically allocated storage. You can use the %SIZE built-in function to ensure that the allocated size matches the size of this variable. If the storage being allocated will not be shared between programs, you can use the Allocate Storage procedure. This procedure returns a pointer to the allocated storage. This allows you to use the storage without using the Retrieve Named Storage procedure.

To allocate array storage, use the Allocate Named Array Storage procedure. You specify an initial number of elements and an optional increment value. When you specify an increment value and attempt to retrieve an array element that falls outside the current allocation, additional storage is automatically allocated. The Allocate Named Storage

procedure also allows you to set up a heading area for your array. This area typically contains information associated with the array, such as the current element count.

Using the Storage Toolkit

Storage allocated using the Storage Toolkit can be retrieved and sorted using Storage Toolkit procedures. When your program no longer needs storage allocated with the Storage Toolkit, the storage should be deallocated. The procedures that support these functions are the retrieval, deallocation, and sorting procedures summarized in Figure 1.

To retrieve storage allocated by the Storage Toolkit, you need to set up pointer- based variables. When defining a field as based, you specify the Based keyword and supply a basing pointer. The value of a variable defined using the Based keyword is stored at the location pointed to by its basing pointer. The TstStgTkt demonstration procedure contains an example that shows how to define and use based variables.

The Storage Toolkit has three procedures that allow you to retrieve allocated storage. The first is the Retrieve Named Storage procedure. This procedure allows you to retrieve storage by using the name assigned when the storage was allocated. This procedure returns a pointer to the allocated storage. The second procedure is the Retrieve Named Array Storage procedure, which allows you to retrieve a pointer to an array element or an array’s heading area. The third procedure is the Retrieve LDA Storage procedure. This procedure is used by a submitted job to retrieve a pointer to a value that was stored in the LDA by its submitting job using the Allocate Named Storage procedure.

The Storage Toolkit provides support for sorting arrays created by the Allocate Named Array Storage procedure. The Sort Named Array Storage procedure checks to see if the array’s storage is contiguous. If the array is not contiguous, Sort Named Array Storage moves the array to a contiguous block of storage. Sort Named Array Storage then sorts the array using the C library QSORT function. One of the parameters passed to Sort Named Array Storage is a procedure pointer to a user-supplied comparison procedure. The comparison procedure compares two elements of the array and returns a value indicating whether the first element passed to it is less than, equal to, or greater than the second element passed to it. A description of the parameters passed to the user-supplied comparison procedure is contained in the heading description of the Sort Named Array Storage prototype. Look at the UsrSrt procedure in the TstStgTkt module to see an example of a user-supplied comparison procedure.

Keeping Track of Storage

The Storage Toolkit has a user index to store information about allocated storage. A user index allows information to be stored and retrieved using a key. User indexes are similar to keyed database files, but they are a little faster because they have less overhead than a file does. Each entry in a user index can contain up to 2,000 bytes of fixed- or varying-length information. User indexes are created using system APIs. The Storage Toolkit contains a module with procedures to support the user index APIs. These procedures provide an interface to the user index APIs that makes them easier to use.

Putting Away the Toolkit

Software toolkits such as the Storage Toolkit allow programmers to focus on the business problem presented to them rather than on the logic needed to develop common coding tasks. This removes the time-consuming burden of having to reinvent common procedures every time an application is developed. This has the net result of giving your shop a much- needed boost in the area of time management.

Related materials

• AS/400 Information Center: publib.boulder.ibm.com/pubs/html/as400/ic2924/info/index.htm

• C Library Reference (SC09-2119-00, CD-ROM QBJATN00)

Procedure Type Description

AlcNamArrStg Allocation Allocates named array storage, including heading area AlcNamStg Allocation Allocates named storage
AlcStg Allocation Allocates storage and returns pointer to storage DlcNamStg Deallocation Deallocates named storage
DlcStg Deallocation Deallocates storage allocated by AlcStg
GetStgLog Logging Retrieves the default storage logging flag
SetStgLog Logging Sets the default storage logging flag
RtvNamStg Retrieval Retrieves a pointer to allocated named storage RtvNamArrStg Retrieval Retrieves a pointer to an array element or array heading area RtvLDAStg Retrieval Retrieves named LDA storage in a submitted job SrtNamArrStg Sort Sorts named array storage using user-supplied procedure AddIdxEnt User index Writes a user index entry
CrtUsrIdx User index Creates a user index
DltUsrIdx User index Deletes a user index
RmvIdxEnt User index Removes a user index entry
RtvIdxEnt User index Retrieves a user index entry
RtvUsrIdxAtt User index Retrieves information about a user index
CrtUsrSpc User space Creates a user space
DltUsrSpc User space Deletes a user space
RtvUsrSpc User space Retrieves a user space
RtvUsrSpcAtt User space Retrieves information about a user space
RtvUsrSpcPtr User space Retrieves a pointer to the beginning of a user space’s data

Figure 1: This is a summary of procedures included with the Storage Toolkit.

DAVID MORRIS
David Morris has worked with and written about a variety of technologies, including ILE, RPG, business intelligence, SQL, security, and genetic programming. Today, David is developing Web applications that run on the iSeries using RPG, Java, and XML as well as writing about these technologies for technical journals.

MC Press books written by David Morris available now on the MC Press Bookstore.

 

XML for eServer i5 and iSeries XML for eServer i5 and iSeries

In this book, you will learn about Extensible Markup Language (XML), but with an IBM eServer i5/iSeries twist.

List Price $64.95
Now On Sale
 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: